473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

An easy question about GetType()

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
Oct 2 '07 #1
5 1445
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
Oct 2 '07 #2
"TonyJ" <jo*****************@telia.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
[...]
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.
Oct 2 '07 #3
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
"Marc Gravell" <ma**********@gmail.comskrev i meddelandet
news:Oh**************@TK2MSFTNGP05.phx.gbl...
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


Oct 2 '07 #4
On Oct 2, 12:50 pm, "TonyJ" <johansson.anders...@telia.comwrote:
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

Oct 2 '07 #5
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
Oct 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ron | last post by:
Hi, I am currently using the base class GetType() in the following way and was woundering if there was a different way of looking at types and doing a comparison for equality. Thanks Ron ...
0
by: Jax | last post by:
I'm having difficulty with creating dynamic types, TypeBuilder is attained through module builder which in turn comes from assembly builder. I assumed that if you save the assembly you'd save the...
2
by: david.kao | last post by:
I have web service always return a string in Xml format, and I have the code at client side translates this return string into correct value. For example: my web service returns a <ret...
6
by: Jim Bancroft | last post by:
Hi everyone, I'm having some trouble with the code below. I receive a compile-time error on the second line saying "; expected": private static void myTestFunction(long myLong) { ...
8
by: Rachel Suddeth | last post by:
I'm trying to use reflection to create an object at runtime where the type is given by a string. I thought we should be able to do that, but so far it's not working. I'm trying to work on...
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
4
by: Greg Burns | last post by:
What the difference between these two? System.Type.GetType("System.Int32") and GetType(Integer) Or more specifically, why does GetType(Integer) work, but not System.Type.GetType(Integer)?...
3
by: Joe Adams | last post by:
Hi All, How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>) I need to now if the <MyType> is the same type of class as the <GenericType> without having to add the generic type...
3
by: b69316 | last post by:
Hi, I am a little confused on something. I am using DevC++. I am working on a project to make a "dinosaur walk across a map". I am not sure about the distance formula....i have no clue how to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.