An easy question about GetType()

  • Thread starter Thread starter TonyJ
  • Start date Start date
T

TonyJ

Hello!!

This first expression return System.Int32. The type that is returned is a
string. This is perfect understandable.
Console.WriteLine(7.GetType().FullName);

In this second expression is the same returned System.Int32. The type that
is returned is System.RuntimeType
Console.WriteLine(7.GetType());

Now to my question how can the System.Int32 be written when the type is
System.RuntimeType

//Tony
 
No; 7.GetType() returns a Type instance, representing System.Int32.
There are different variants of Type, however, depending on the
scenario. In this case, Type instance (representing System.Int32)
happens to be a RuntimeType. However, it still *represents*
System.Int32.

Does that make sense? RuntimeType is the Type of the Type ;-p

If the question is about what gets written, then it is because
RuntimeType.ToString() has been overridden to return the name of the
represented type.

Marc
 
TonyJ said:
[...]
In this second expression is the same returned System.Int32. The type that
is returned is System.RuntimeType
Console.WriteLine(7.GetType());

Console.WritelLine calls the ToString method on its Object argument, so
you are actually seeing 7.GetType().ToString(), which happens to return
"System.RuntimeType".
Now to my question how can the System.Int32 be written when the type is
System.RuntimeType

In your other example, Console.WriteLine(7.GetType().FullName);, you are
invoking the property FullName on the result of GetType, which does not
return the same value as the method ToString of the same object.
 
Hello!!

You say that
"RuntimeType.ToString() has been overridden to return the name of the
represented type."

I haven't overridden anything not even ToString(). What do you mean?

//Tony
 
I haven't overridden anything not even ToString().
But then, you didn't write RuntimeType either...

Where are you even seeing RuntimeType? I've just tried:
Console.WriteLine(7.GetType());
Console.WriteLine(7.GetType().ToString());
Console.WriteLine(7.GetType().FullName);

If you are using 1.1, then you're on your own - I burnt all my 1.1
tools a while ago ;-p
On 2.0 (which covers 3.0) and 3.5 it outputs System.Int32 three
times... I can only assume that you are looking in the IDE (immediate
window, or hover, etc) - in which case it will tell you about the
instance that is being queried, including it's Type (which in this
case *happens* to be a Type itself [specifically, RuntimeType], but
that is coincidence).

So where are you seeing "RuntimeType"?

Marc
 
You say that
"RuntimeType.ToString() has been overridden to return the name of the
represented type."

I haven't overridden anything not even ToString(). What do you mean?

The type "System.RuntimeType" has overridden ToString(). That
overridden method is what's being called when you write:

Console.WriteLine(7.GetType());

I'm still not entirely clear what you're trying to do. Could you give
more description of what you're trying to achieve?

Jon
 
Back
Top