473,796 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetType().ToStr ing()

Hi,

When I run this program:

static void Main(string[] args)
{
SortedDictionar y<string, List<DateTime>c omplex =
new SortedDictionar y<string, List<DateTime>> ();
Console.WriteLi ne(complex.GetT ype().Name);
Console.WriteLi ne(complex.GetT ype().ToString( ));

}

The output is:

SortedDictionar y`2
System.Collecti ons.Generic.Sor tedDictionary`2[System.String,S ystem.Collectio ns.Generic.List `1[System.DateTime]]

What is the ''2' at the end of "SortedDictiona ry`2"?

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

Thank you,
Max

Oct 3 '07 #1
5 4116
Max2006 wrote:
[...]
static void Main(string[] args)
{
SortedDictionar y<string, List<DateTime>c omplex =
new SortedDictionar y<string, List<DateTime>> ();

Console.WriteLi ne(complex.GetT ype().Name);
Console.WriteLi ne(complex.GetT ype().ToString( ));

}

The output is:

SortedDictionar y`2
System.Collecti ons.Generic.Sor tedDictionary`2[System.String,S ystem.Collectio ns.Generic.List `1[System.DateTime]]

What is the ''2' at the end of "SortedDictiona ry`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
(SortedDictiona ry<string, List<DateTime>> ) ?
There are others who know more about the specifics, but the basic issue
is that SortedDictionar y<string, List<DateTime>i sn't actually a type
at all. It's a way of declaring a specific instance of a type using the
generic SortedDictionar y<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 "SortedDictiona ry`2".

Pete
Oct 3 '07 #2
On Oct 2, 9:55 pm, "Max2006" <alanal...@news group.nospamwro te:
The output is:

SortedDictionar y`2
System.Collecti ons.Generic.Sor tedDictionary`2[System.String,S ystem.Collecti* ons.Generic.Lis t`1[System.DateTime]]

What is the ''2' at the end of "SortedDictiona ry`2"?

Why can't I receive the actual type that I declared
(SortedDictiona ry<string, List<DateTime>> ) ?
I believe the "'2" means "two type parameter" (It would be completely
legal to also have a SortedDictionar y which take one or three type
parameters)

Oct 3 '07 #3
ja**********@gm ail.com wrote:
On Oct 2, 9:55 pm, "Max2006" <alanal...@news group.nospamwro te:
>The output is:

SortedDictiona ry`2
System.Collect ions.Generic.So rtedDictionary` 2[System.String,S ystem.Collecti* ons.Generic.Lis t`1[System.DateTime]]

What is the ''2' at the end of "SortedDictiona ry`2"?

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

I believe the "'2" means "two type parameter" (It would be completely
legal to also have a SortedDictionar y 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...@Nn OwSlPiAnMk.comw rote:
I believe the "'2" means "two type parameter" (It would be completely
legal to also have a SortedDictionar y 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.Collecti ons.Generic;

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

ShowType(listOf String);
ShowType(listOf Int);
}

static void ShowType(Type t)
{
string name = t.FullName;
int tick = name.IndexOf('` ');
if (tick != -1)
{
name = name.Substring( 0, tick);
Type[] args = t.GetGenericArg uments();
// 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.WriteLi ne(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
GetGenericTypeD efinition() 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
46053
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 foreach(System.Web.UI.Control ctl in this.Controls) {
3
4085
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 changes. I now have a method that takes an Excel.Range as a parameter. My problem now is that when I use Excel's function wizard, there are thre more functions available, namely ToString, GetHashCode & GetType. I don't want them. How can I get...
1
7010
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: string msParcoWebSvc = mappConfig.GetValue("ParcoWebServiceURL",
1
4815
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 how to fix this please let me know. c.setComponentProperties(Request.QueryString,"1"); classname = c.cSrc; t = System.Type.GetType(classname); uc = (UserControl)Activator.CreateInstance(t); ctl =
5
2268
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 = "otherText" xmlSave("C:\folder\file.xml", mySettings) Here is the sub: Public Shared Sub xmlSave(ByVal path As String, ByVal config As
4
2870
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 is passed in as a known type, but gappWord is has a scope of the class, so a test of the type is valid (making believe the sub does not know the pre-ordained type). TypeOf returns Word.Application. TypeName returns ApplicationClass.
1
1723
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 classes etc. However if I call ReturnValue(myClass.CompanyName) and CompanyName is Nothing then it crash where it shoud return SqlTypes.SqlString.Null Private _CompanyName As String
5
1461
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 the same returned System.Int32. The type that is returned is System.RuntimeType Console.WriteLine(7.GetType());
2
3486
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........ ............................................................................................................... namespace x {. public interface ISection {
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.