Display representation of generic types

P

proxyuser

Actually I'm not sure if this has to do specifically with generic types, but
I don't understand the output from the following programlet, which is:

System.Collections.Generic.List`1[TestApp.Program+A]

What are the extraneous characters doing there?

class Program
{

internal class A

{

}

static void Main( string[] args )

{

List<A> list = new List<A>();

Console.WriteLine(list.GetType());

Console.ReadLine();

}

}
 
G

Gregory A. Beamer

Actually I'm not sure if this has to do specifically with generic
types, but I don't understand the output from the following
programlet, which is:

System.Collections.Generic.List`1[TestApp.Program+A]

What are the extraneous characters doing there?

SNIPPED CODE


When you GetType() off a generic, you are seeing both the type (the
generic list) and some information about what is actually held in the
list. That is the "extraneous" bits you see.

I can rewrite the majority of your code by looking at this (without
looking at what I snipped out):

System.Collections.Generic.List`1[TestApp.Program+A]

First, the actual type is an internal class A inside a class called
Program, which is inside the namespace TestApp.

//From [TestApp.Program+A]
namespace TestApp
{
public class Program
{
internal class A
{
}
}
}

With A being inside Program, I can tell the running routine is inside
program, so it would look something like this:

//From [TestApp.Program+A]
namespace TestApp
{
public class Program
{
internal class A
{
}

//Since you are outputting GetType()
//I know you have a Main() routine
static void Main(string[] args)
{
//From System.Collections.Generic.List
//and Program+A, focusing on the +
List<A> list = new List<A>();

//The line that outputs what you have
Console.WriteLine(list.GetType().ToString());

//Stop so you can read from Console
Console.Read();
}
}
}

make sense? I hope this points out that the extraneous stuff. I am not
sure what the 1 means here.


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
B

Ben Voigt [C++ MVP]

Gregory said:
Actually I'm not sure if this has to do specifically with generic
types, but I don't understand the output from the following
programlet, which is:

System.Collections.Generic.List`1[TestApp.Program+A]
make sense? I hope this points out that the extraneous stuff. I am not
sure what the 1 means here.

List`1 means the generic List class that accepts one type parameter. You
can overload on the number of generic parameters (think IComparable vs
IComparable<T>) so they need to have different names.

The reason it doesn't show up as List<TestApp.Program+A> is because it's not
 
G

Gregory A. Beamer

make sense? I hope this points out that the extraneous stuff. I am
not sure what the 1 means here.

List`1 means the generic List class that accepts one type parameter.
You can overload on the number of generic parameters (think
IComparable vs IComparable<T>) so they need to have different names.

The reason it doesn't show up as List<TestApp.Program+A> is because
it's not language dependent, and they had to pick one of C# = List<T>,
VB = List(Of T), MSIL = List`1[T] and they picked the MSIL syntax.


Makes sense. Thanks for responding. I could read the rest of it quite
easily. ;-)


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

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