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