You need to use
this.GetType().Name
to obtain the
run-time type of the instance used for a call and get its simple (not fully-qualified) name. So, it will be:
Console.WriteLine(string.Format("The run-time type of the calling instance is {0}", this.GetType().Name));
Console.WriteLine("The run-time type of the calling instance is {0}", this.GetType().Name);
It's important to understand that you use instance (non-static) methods. These method has additional (hidden, implicit) parameter called "this", used to carry the instance passed as specified by your variable name on the left part of '.' (dot) in
obj1.Methodtoimp();
or
obj2.Methodtoimp();
that is,
obj1
,
obj2
or the like.
(Simplified form of
System.Console.WriteLine
without using
string.Format
is possible because of this overload:
http://msdn.microsoft.com/en-us/library/828t9b9h.aspx[
^].)
I would also want to warn you that using the type object, type name or something like this to define logic of your code would be the big abuse defeating the purpose of OOP. With OOP, you should use
late binding based on virtual and overridden methods and/or interfaces. Well, so far, you are not doing anything bad…
—SA