Inheritance chain with overriding and shading

So, over the past two days, I've been to several interviews for girls' positions, and two of the companies have asked a very similar question. For the most part I think I understand what is happening and why, there are some things that I don’t know why they work the way they work, and finally, apart from explanations, I would like someone to be able to direct me to those parts C#

that are responsible for this behavior, as I would like to read this topic in more detail.

First, a question from the interview:

class Program
{


    static void Main(string[] args)
    {
        A a = new B();
        Console.WriteLine("A a = new B() => a.PrintName Result");
        a.PrintName(); //Output "BBB" as expected
        Console.WriteLine("----------------");
        A c = new C();
        Console.WriteLine("A c = new C() => c.PrintName Result");
        c.PrintName(); //Otuput is again "BBB". What about shadowing?
    }

}

public class A
{
    public virtual void PrintName()
    {
        Console.WriteLine("AAA");
    }
}

public class B : A
{
    public override void PrintName()
    {
        Console.WriteLine("BBB");
    }
}

public class C : B
{
    public new void PrintName()
    {
        Console.WriteLine("CCC");
    }
}

      

The questions are about the code in the method Main

, where, as you can see, the variable is a

declared with a type a

, but then created using new B()

, similar happens here A c = new C();

, but as you can see in class C

instead of overriding the method PrintName

, it was declared using the keyword new

which is from mine (clearly incomplete understanding) should still provide an implementation in class C

when this variable is created of this type, however, as you can see in the comments after the method call, the result isBBB

for both cases. So my first question is what is the logic behind this behavior. And also as a side question, since during the interview I was not sure of the correct answer, I tried to apply some logic which was quite simple and stated that according to (if I am not mistaken) MSDN recommendation, it is better to use keyword var

instead of specific types and in fact, unless we explicitly indicate the type of the variable like this:

 var c = new C();
 Console.WriteLine("A c = new C() => c.PrintName Result");
 c.PrintName(); //Otuput is "CCC"

      

we get it with confidence CCC

. This second question causes more curiosity, since, obviously, the use of var

and a particular type is not always interchangeable, and, as we can see, based on this, we can get a completely different result. I don't use this as some kind of excuse or anything like that. If you are writing code that is expected to know what your code is doing, so perhaps these guidelines implicitly assume that the developer is aware of this and can make their choices accordingly, or in the end this is more of a trick. to see if the candidate knows it and at the end of the day usevar

is always preferred and this kind of coding is not recommended (I wish it were), or is there a valid case where we want to take advantage of this exact behavior that I described?

+3


source to share





All Articles