GetType().ToString()

M

Max2006

Hi,



When I run this program:



static void Main(string[] args)
{
SortedDictionary<string, List<DateTime>> complex =
new SortedDictionary<string, List<DateTime>>();


Console.WriteLine(complex.GetType().Name);
Console.WriteLine(complex.GetType().ToString());

}



The output is:



SortedDictionary`2
System.Collections.Generic.SortedDictionary`2[System.String,System.Collections.Generic.List`1[System.DateTime]]



What is the ''2' at the end of "SortedDictionary`2"?

Why can't I receive the actual type that I declared
(SortedDictionary<string, List<DateTime>>) ?



Thank you,
Max
 
P

Peter Duniho

Max2006 said:
[...]
static void Main(string[] args)
{
SortedDictionary<string, List<DateTime>> complex =
new SortedDictionary<string, List<DateTime>>();

Console.WriteLine(complex.GetType().Name);
Console.WriteLine(complex.GetType().ToString());

}

The output is:

SortedDictionary`2
System.Collections.Generic.SortedDictionary`2[System.String,System.Collections.Generic.List`1[System.DateTime]]

What is the ''2' at the end of "SortedDictionary`2"?

I think it's 1 more than the "`1" at the end of "List`1". :)
Why can't I receive the actual type that I declared
(SortedDictionary<string, List<DateTime>>) ?

There are others who know more about the specifics, but the basic issue
is that SortedDictionary<string, List<DateTime>> isn't actually a type
at all. It's a way of declaring a specific instance of a type using the
generic SortedDictionary<> class.

The compiler, seeing that declaration, has to generate an actual type
that can be used. The type it generates is named as you see it in the
output from your test application. That is, the actual concrete
instance of the type you've implicitly created with your declaration is
named "SortedDictionary`2".

Pete
 
J

james.curran

The output is:

SortedDictionary`2
System.Collections.Generic.SortedDictionary`2[System.String,System.Collecti­ons.Generic.List`1[System.DateTime]]

What is the ''2' at the end of "SortedDictionary`2"?

Why can't I receive the actual type that I declared
(SortedDictionary<string, List<DateTime>>) ?

I believe the "'2" means "two type parameter" (It would be completely
legal to also have a SortedDictionary which take one or three type
parameters)
 
P

Peter Duniho

The output is:

SortedDictionary`2
System.Collections.Generic.SortedDictionary`2[System.String,System.Collecti­ons.Generic.List`1[System.DateTime]]

What is the ''2' at the end of "SortedDictionary`2"?

Why can't I receive the actual type that I declared
(SortedDictionary<string, List<DateTime>>) ?

I believe the "'2" means "two type parameter" (It would be completely
legal to also have a SortedDictionary which take one or three type
parameters)

Thank you. You certainly saved this thread from my own wild
speculations, and your answer makes perfect sense (and in fact is borne
out in test code with varying numbers of generic parameters). :)

I find it interesting to see that the name of the type returned is
identical no matter what the generic parameter is. I guess that's
because there's just one copy of the compile generic implementation?

The type reference is obviously not the same, since the ToString()
result is different according to the actual concrete instance of the
generic class. But multiple references wind up with the same Name property.

I love generics, but clearly I could use a little edumacation on the
topic. :)

Pete
 
J

Jon Skeet [C# MVP]

Thank you. You certainly saved this thread from my own wild
speculations, and your answer makes perfect sense (and in fact is borne
out in test code with varying numbers of generic parameters). :)

I find it interesting to see that the name of the type returned is
identical no matter what the generic parameter is. I guess that's
because there's just one copy of the compile generic implementation?

Type.ToString() *could* provide that information, but it would be a
little bit more expensive. Personally I think that the expensive would
be worth it, but there we go... You can do it by hand though. For
instance:

using System;
using System.IO;
using System.Collections.Generic;

public class Test
{
static void Main()
{
Type listOfString = new List<string>().GetType();
Type listOfInt = new List<int>().GetType();

ShowType(listOfString);
ShowType(listOfInt);
}

static void ShowType(Type t)
{
string name = t.FullName;
int tick = name.IndexOf('`');
if (tick != -1)
{
name = name.Substring(0, tick);
Type[] args = t.GetGenericArguments();
// So much easier with LINQ...
string[] argNames = new string[args.Length];
for (int i=0; i < args.Length; i++)
{
argNames = args.Name;
}
name = string.Format("{0}<{1}>", name, string.Join(",",
argNames));
}
Console.WriteLine(name);
}
}
The type reference is obviously not the same, since the ToString()
result is different according to the actual concrete instance of the
generic class. But multiple references wind up with the same Name property.

Although the type references themselves aren't the same, if you call
GetGenericTypeDefinition() on both of them, *those* references will be
the same.
I love generics, but clearly I could use a little edumacation on the
topic. :)

Reflection on generics is somewhat messy, unfortunately. It's even
worse when a type which is "open" at compile-time is "closed" at
runtime...

I can't resist a plug at this point, I'm afraid: try the generics
chapter of my book and see if it helps. It's already available for
early access:
http://manning.com/skeet

Jon
 
J

Jialiang Ge [MSFT]

Hello,

The '2' in SortedDictionary`2 does mean the parameter number. For instance,
the name of Container<String> is Container'1. Here is an article that is
talking about the reflecting on Generic Types:
http://msdn.microsoft.com/msdnmag/issues/05/12/PureC/

If you have any other concen or need anything else, please feel free to let
me know.

Sincerely,
Jialiang Ge ([email protected], remove ¡®online.¡¯)
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box ¡°Tools/Options/Read: Get 300 headers at a time¡±
to see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided ¡°AS IS¡± with no warranties, and confers no
rights.
 
J

Jialiang Ge [MSFT]

Hi Max,

If you need further assistance, feel free to let me know. I will be more
than happy to be of assistance.

Have a great day!

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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