473,382 Members | 1,380 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,382 software developers and data experts.

GetType().ToString()

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

Oct 3 '07 #1
5 4092
Max2006 wrote:
[...]
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
Oct 3 '07 #2
On Oct 2, 9:55 pm, "Max2006" <alanal...@newsgroup.nospamwrote:
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)

Oct 3 '07 #3
ja**********@gmail.com wrote:
On Oct 2, 9:55 pm, "Max2006" <alanal...@newsgroup.nospamwrote:
>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
Oct 3 '07 #4
On Oct 3, 5:13 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
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?
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[i] = args[i].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

Oct 3 '07 #5
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 (ji****@online.microsoft.com, 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.

Oct 5 '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 ...
3
by: Fredrik Wahlgren | last post by:
Hi I found an interesting piece of code which shows how to makee an Excel automation add-in at http://www.codeproject.com/dotnet/excelnetauto.asp. I decide to experiment with it and i made a few...
1
by: karunakar | last post by:
this is vb.net CODE : string msParcoWebSvc = mappConfig.GetValue("ParcoWebServiceURL", GetType(String)).ToString How to change code in C# : I was done like this but iam getting error: ...
1
by: Josh | last post by:
Hi Guys, I have hit a wall with the my project... The problem is that i need to get the type of a usercontrol that is not in the assembly of the page that is tryiong to call it. if anyone knows of...
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: Howard Kaikow | last post by:
For the code below, for both appWord and gappWord, I get the error "Public member 'GetType' on type 'ApplicationClass' not found" I realize the test for appWord is superflous as the parameter...
1
by: Nicolas | last post by:
I want to return the proper Null to the SQL Database if my property is nothing Also I thought about creating a function so I don't have to put so many if statement for each property for each...
5
by: TonyJ | last post by:
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...
2
by: nibin | last post by:
pls help me it urgent i m facing a problem with my vb.net code this is the code equivalent in c# this is the interface...........
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.