I have two classes. In one is nested another class.
class Person : ICloneable
{
public string name;
public City city;
public object Clone()
{
return this.MemberwiseClone();
}
}
class City
{
public string name;
public City( string _n)
{
name = _n;
}
}
In Person class is clone method to make shallow copy. When I made clone of class Person, I got clone of class Person and clone of nested class City.
Person person = new Person()
{
name = "John",
city = new City("London")
};
Person personNew = (Person)person.Clone();
Console.WriteLine( $"{personNew.name}\t\n{personNew.city.name}");
Result:
John
London
I expected to get value of City class as null because I made shallow copy of Person class but it looks like deep copy. Why ?