Search code examples
c#radix

About base in C#


namespace contest
{
    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
        }
    }

    class A {
        public A() {
            k();
        }
        private void k() {
            Console.WriteLine(base.GetType().Name);
        }        
    }

    class B : A {

    }
}

Can someone tell me why it outputs "B" instead of "Object", doesn't base.GetType() get A's parent object therefore the root Object?

Thanks a lot


Solution

  • That happens because

    base.GetType()
    

    means "call the GetType() method of parent class", though you haven't overriden it. Thus base.GetType() as well as this.GetType() would always return class B