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

VulpesPosted Oct 4, 2014, 6:28 AM
However, this only works because 'base' here - as previously discussed - is really an instance of B. If it were an instance of A you'd get a NullReferenceException because System.Object doesn't have a base class.
There is a way to always get 'Object' without using typeof(A):
The output is:
Object
Object
However, we're having to use reflection to do this and so the code is relatively slow to execute.
Max AlbishPosted Oct 4, 2014, 9:07 AM
Max AlbishPosted Oct 3, 2014, 10:20 PM
VulpesPosted Oct 3, 2014, 5:22 AM
Max PowerPosted Oct 2, 2014, 11:46 PM
VulpesPosted Oct 2, 2014, 9:11 AM
The 'base' keyword doesn't alter the type of the current instance - it merely enables you to access base class members through that instance which might otherwise be hidden.
So, in this case, the current instance is of type B (even though its accessing code in its base class A) and so that's what base.GetType().Name returns.
If you changed your Main method to this:
you'd find that the second line printed out A because that's now the runtime type of the current instance.