I have a base class and a derived class as bellow
public class animal
{
public string name { get; set; }
}
public class dog : animal
{
public int age { get; set; }
public string type { get; set; }
}
animal a = new animal();
dog d = new dog();
a = d; //compiled
d = a; //Error:Cannot implicitly convert type 'animal' to 'dog'.
d = (dog)a; // compiled
What happen internally that derived class can assigned to base but doing the reverse explicit conversion is required? Same result found even both base and derived class contains same member.
All dogs are animals, but not all animals are dogs. Implicit conversion is not allowed because your a might not actually be a d.