0

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:

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
xaisoft
  • 3,343
  • 8
  • 44
  • 72
  • 4
    You're confusing objects and references. `car` is a reference, `car2` is a reference, and both refer to the same object (for which there isn't a name in the source code!). –  Oct 11 '13 at 15:08
  • If you don't want car2 to be a copy of car, you need to make it new. – PiousVenom Oct 11 '13 at 15:08
  • If you never want to pass `Car` by reference. You may want to consider making it a `struct` instead of a `class`. – Khan Oct 11 '13 at 15:11
  • If you want a **new** `car2` with the color of `car`, you should do `Car car2 = new Car {Color = car.Color}` – Ahmed KRAIEM Oct 11 '13 at 15:13

3 Answers3

2

When I do this resharper tells me it is unnecessar

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

When you want car1 and car2 to be different colors then they should be different instances. And that does require 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:

public static void Format(Car car)
{
    car.Color = "Red";
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • 2
    I think the problem may well be with the way the OP was trying to do it. We can't see the code that R# was warning about... – Jon Skeet Oct 11 '13 at 15:08
  • 5
    He was probably writing `Car car2 = new Car(); car2 = car;`, in which case Resharper would be right. – SLaks Oct 11 '13 at 15:09
  • @JonSkeet - I will post the code Resharper was complaing about. It is basically what SLaks said. – xaisoft Oct 11 '13 at 15:11
  • I changed the wording a little. Don't have R# here. – H H Oct 11 '13 at 15:12
  • @HenkHolterman - How can I create two instances of the same object, but point to different references. – xaisoft Oct 11 '13 at 16:30
  • You don't point _to references_, you point _with references_. The crucial part to understand is that `car` and `car2` are not `Car` objects, they are references to the (same single) nameless instance on the heap. – H H Oct 11 '13 at 17:27
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.

You could define a copy constructor for your Car class:

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

Then use it like this:

Car car2 = new Car(car);
JAB
  • 313
  • 2
  • 10
0

You can make Car a struct

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.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • careful, MS warns against using ICloneable http://stackoverflow.com/questions/699210/why-should-i-implement-icloneable-in-c – dferraro Oct 11 '13 at 15:20