Usually people want to make a duplicate of an object into a new object so that.
List<Car> cars = new List<Car> { new Car(), new Car() }
var b = cars[0];
var a = new Car { Brand = "Something", Price = 123}
cars[0] = a.Clone();
in this case the:
b.Brand => ""
b.Price => 0
I'm looking for a way create a "copy to reference" extension and I've been unsuccessful. This is what I'm trying to accomplish.
List<Car> cars = new List<Car> { new Car(), new Car() }
var b = cars[0];
var a = new Car { Brand = "Something", Price = 123}
a.CopyTo(ref cars[0]);
Outputs:
b.Brand => "Something"
b.Price => 123
so I'm not really replacing the object in the List but just copying to it by reference. Well I can do it manually (property be property) but I was looking for something more general (that could be applied to every object).
I know this is possible to accomplish with a method like
public static void CopyTo(this Car source, ref Car target)
{
target.Brand = source.Brand;
target.Price = source.Price;
}
but I wanted something more general, like going through all the variables in the object (automatically).