如何通过更改另一个来阻止更改对象?

时间:2021-06-04 10:00:40

I understand that these two objects point to the same reference, but I don't want that to be the case, so I am a bit confused on how to prevent one from changing the other. Do I need to declare a band new Car object, for example Car car2 = new Car();? When I do this resharper tells me it is unnecessary. Here is my code:

我知道这两个对象指向相同的引用,但我不希望如此,所以我对如何防止一个更改另一个有点困惑。我是否需要声明一个乐队新的Car对象,例如Car car2 = new Car();?当我这样做时,resharper告诉我这是不必要的。这是我的代码:

void Main()
{
    Car car = new Car { Color = "Blue"};

    Console.WriteLine(car.Color);

    //Do I need Car car2 = new Car();
    //car2 = car; //Confused.

    Car car2 = car;

    Console.WriteLine(car2.Color);

    car = Format(car);

    Console.WriteLine(car.Color);
    Console.WriteLine(car2.Color); //How can I prevent car2 from changing color?
}

// Define other methods and classes here
public class Car
{
    public string Color {get;set;}
}

public static Car Format(Car car)
{
    car.Color = "Red";

    return car;
}

 var car2= new Car();
 car2 = car;

 car = Format(car); //This changes car and car2

3 个解决方案

#1


2  

When I do this resharper tells me it is unnecessar

当我这样做时,resharper告诉我这是不必要的

Then Resharper would be wrong in this case. But it depends on your exact code.

那么Resharper在这种情况下会出错。但这取决于您的确切代码。

When you want car1 and car2 to be different colors then they should be different instances. And that does require Car car2 = new Car();

当你想要car1和car2有不同的颜色时,它们应该是不同的实例。这确实需要Car car2 = new Car();

Car is a class is a reference-type.
In your posted code there is only 1 instance, so there can only be one color.

汽车是一类是参考型。在您发布的代码中只有一个实例,因此只能有一种颜色。

Note that for the same reason your Format method does not need to return anything, the following will do exactly the same:

请注意,出于同样的原因,您的Format方法不需要返回任何内容,以下内容将完全相同:

public static void Format(Car car)
{
    car.Color = "Red";
}

#2


1  

As you mention, car2 and car refer to the same object. It sounds as if you don't want that, so you will need to create a copy of car and call it car2.

如你所述,car2和car指的是同一个对象。听起来好像你不想要那样,所以你需要创建一个汽车副本并称之为car2。

You could define a copy constructor for your Car class:

您可以为Car类定义复制构造函数:

public Car(Car car)
{
    this.Color = car.Color;
}

Then use it like this:

然后像这样使用它:

Car car2 = new Car(car);

#3


0  

You can make Car a struct

你可以把Car变成一个结构

public struct Car
{
    public string Color { get; set; }
}

or make Car ICloneable or any other of the dozen ways to copy the object so you can easily have another instance. We need to see the code that shows the warning so we could figure out the problem. I'm not seeing the warning.

或制作Car ICloneable或其他十几种复制对象的方法,以便您可以轻松拥有另一个实例。我们需要查看显示警告的代码,以便我们找出问题所在。我没有看到警告。

#1


2  

When I do this resharper tells me it is unnecessar

当我这样做时,resharper告诉我这是不必要的

Then Resharper would be wrong in this case. But it depends on your exact code.

那么Resharper在这种情况下会出错。但这取决于您的确切代码。

When you want car1 and car2 to be different colors then they should be different instances. And that does require Car car2 = new Car();

当你想要car1和car2有不同的颜色时,它们应该是不同的实例。这确实需要Car car2 = new Car();

Car is a class is a reference-type.
In your posted code there is only 1 instance, so there can only be one color.

汽车是一类是参考型。在您发布的代码中只有一个实例,因此只能有一种颜色。

Note that for the same reason your Format method does not need to return anything, the following will do exactly the same:

请注意,出于同样的原因,您的Format方法不需要返回任何内容,以下内容将完全相同:

public static void Format(Car car)
{
    car.Color = "Red";
}

#2


1  

As you mention, car2 and car refer to the same object. It sounds as if you don't want that, so you will need to create a copy of car and call it car2.

如你所述,car2和car指的是同一个对象。听起来好像你不想要那样,所以你需要创建一个汽车副本并称之为car2。

You could define a copy constructor for your Car class:

您可以为Car类定义复制构造函数:

public Car(Car car)
{
    this.Color = car.Color;
}

Then use it like this:

然后像这样使用它:

Car car2 = new Car(car);

#3


0  

You can make Car a struct

你可以把Car变成一个结构

public struct Car
{
    public string Color { get; set; }
}

or make Car ICloneable or any other of the dozen ways to copy the object so you can easily have another instance. We need to see the code that shows the warning so we could figure out the problem. I'm not seeing the warning.

或制作Car ICloneable或其他十几种复制对象的方法,以便您可以轻松拥有另一个实例。我们需要查看显示警告的代码,以便我们找出问题所在。我没有看到警告。