Let's start with the theory.
Suppose there are the following classes:
class A{} class B : A{} class C : B{} Next, we do UpCast :
A a1 = new C(); Is the following statement true: the object a1 is an object of type C, and the base class for it is type A (that is, up the hierarchy)!?
Far from leaving the cash register, imagine what you have added to the code the following:
class A { public virtual void Method() { Console.WriteLine("Method A invoked"); } } class B : A { public new virtual void Method() { Console.WriteLine("Method B invoked"); } } class C : B { public override void Method() { Console.WriteLine("Method C invoked"); } } What will be displayed on the screen?
First of all, it will seem that everything is obvious here, and we will get the output:
Method A invoked Method A invoked Method С invoked Method C invoked But in fact, we get: Method A invoked Method A invoked Method A invoked Method C invoked
Based on this logic, it turns out that my previous statement is not true, and this means that all the same base class for C is B ?
We now turn to another part of the question.
For example, we have the code:
class Program { static void Main(string[] args) { //объект типа класса А A a = new A(); //объект типа класса B B b = new B(); //UpCast, который равен объекту "b" A a1 = b; //UpCast как отдельный объект A a2 = new B(); //DownCast, который равен объекту "а1" B b1 = (B)a1; B b2 = a as B; // вернет Null, т.к. DownCast //без предварительного UpCast не возможен // B b2 = new A(); - невозможно из за безопасности типов //сравниваем b с а1,видим что типы идентичны. Console.WriteLine(b.GetType() == a1.GetType()); //сравниваем а2 с а1,видим что типы идентичны. Console.WriteLine(a2.GetType() == a1.GetType()); //сравниваем b1 и а1,видим что типы идентичны Console.WriteLine(b1.GetType() == a1.GetType()); //Проверяем сами обьекты,вернет True Console.WriteLine(a1.Equals(b)); //вернет False,но реализация этих объектов идентична Console.WriteLine(a2.Equals(a1)); //Вернет True Console.WriteLine(b1.Equals(a1)); Console.ReadKey(); } } class A { } class B : A { } So what happens behind the scenes?
As with UpCast "e \ DownCast" e , two identical objects (or rather two links pointing to the same object) have a different implementation (yes, yes - this is polymorphism). How is this achieved (that is, how does the CLR implement this model of behavior) and how does all this wonder-and-how look in the CLR itself look like? What does "inheritance" inside the CLR look like between types?
