Class Object and override

T

Tony Johansson

Hello!!

You may correct me if I have made any wrong assumptions.

Below I have some simple classes.
When you have this t.ToString() below it's the ToString() method in Object
class that is called
and when you have this t.GetType() below it's the GetType() method in the
Object class that is called.

When you have 123.ToString() it's the ToString method in the string class
that is called. When you have 123.GetType() it's the GetType that is called
in the string class. Is this right understood.


One more question.
I just wonder how can the Object class know what the class is because you
just call a method
ToString or GetType in the Object class. For example when you do
t.ToString() or t.GetType()how can the Object class know that the Test
should be written.


class Test
{}

class myApp
{
public static void Main()
{
Test t = new Test();
Console.WriteLine("ToString: {0}", t.ToString() );
Console.WriteLine("Type: {0}",t.GetType() );

Console.WriteLine("ToString: {0}", 123ToString() );
Console.WriteLine("Type: {0}", 123.GetType() );
}

//Tony
 
T

Tony Johansson

Hello!

Note:
I made a typo in my example program below it should be Object t = new
Test(); and not what is written now Test t = new Test();

//Tony
 
G

Guest

Actually, no.

Assuming that the Test class has a ToString() method overriding the base
class method, it is the Test class ToString() method that will be called
becuase the object referenced by t is actually of type Test even thought is
of type object. The same goes for the GetType() method.

123.ToString() and 123.GetType() would call the methods of the integer class
because 123 is an integer.
 
B

Bjorn Abelli

...
You may correct me if I have made any wrong assumptions.

Yes, you have... ;-)
Below I have some simple classes.
When you have this t.ToString() below it's the
ToString() method in Object class that is called and when you have this
t.GetType()
below it's the GetType() method in the Object class that is called.

Yes, so far so good...
When you have 123.ToString() it's the ToString method in the string class
that is called. When you have 123.GetType() it's the GetType that is
called in the string class. Is this right understood.

Nope, 123 is a literal of type int (System.Int32), which means that the
ToString-method defined in the class System.Int32 that will be called.

As System.Int32 haven't overridden the GetType-method, that is still the
method defined in the System.Object class that will be executed.
One more question.
I just wonder how can the Object class know what the
class is because you just call a method ToString or
GetType in the Object class.

Even if the methods aren't overridden, they nevertheless "belong" to the
actual type of the instance anyway, through inheritance.

That means in simple terms that when you call GetType on an instance of
Int32, you call it on an instance of Int32, not an instance of Object.
For example when you do t.ToString() or t.GetType()how can the Object
class
know that the Test should be written.

Each instance "knows" of what actual type it is itself.

When you call GetType on t, it uses the implementation inherited from
Object, retrieving the runtime type internally, returning that as an
instance of the class Type.

As you then use it in the WriteLine-method, that will in turn invoke the
ToString method from the Type class, in order to get a string representation
it can print.

When you call ToString on t, as it doesn't override ToString, it uses the
default implementation of ToString inherited from Object, which actually
calls GetType().FullName in order to get a "generic" string representation
of the instance.

When you call 123.ToString(), that call makes use of the overriding
implementation of ToString in System.Int32, which involves the use of
NumberFormatters, etc...


HTH.

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top