I have question regarding copying object using = operation and by shallow copy (MemberwiseClone)
I heard earlier that = operator also uses MemberwiseClone behind the scene but when I tested I got different result.
For Example:
class A
{
string Brand;
int Rating;
}
A a1 = new A();
a1.Brand = "ABC";
a1.Rating = 1;
A a2 = a1;
a2.Brand = "PQR";
a2.Rating = 2;
Console.WriteLine("Origional Object");
Console.WriteLine("Brand: {0}, Rating {1}", a1.Brand, a1.Rating); //Prints Brand: PQR, Rating 2
Console.WriteLine("Copied Object");
Console.WriteLine("Brand: {0}, Rating {1}", a2.Brand, a2.Rating); //Prints Brand: PQR, Rating 2
When I copy using MemberwiseClone, I find result as below.
Brand: ABC, Rating 1
Brand: PQR, Rating 2
May I know when I copy object using = then why source object is also getting changed even for changing primitive types in target object where not when copying using MemberwiseClone?