Generics

M

mick

Still dabbling in Generics. Couple of questions

1 . If I pass a generic list into a generic method, how do I
find the type of the objects in the list - cant work out
the syntax.

2. The placeholders that are used like "<T>" cant they be
anything <A> <B> <X> <Z>?

mick
 
K

kndg

Still dabbling in Generics. Couple of questions

1 . If I pass a generic list into a generic method, how do I
find the type of the objects in the list - cant work out
the syntax.

typeof(T) will return the type of T.

static void GenericMethod<T>(List<T> list)
{
Console.WriteLine(typeof(T));
}
2. The placeholders that are used like "<T>" cant they be
anything <A> <B> <X> <Z>?

You can use anything you like.

Regards.
 
M

mick

kndg said:
typeof(T) will return the type of T.

static void GenericMethod<T>(List<T> list)
{
Console.WriteLine(typeof(T));
}

Thanks for that. I faffed around for ages:)
You can use anything you like.

Ok. I`d seen a few examples using different letters and wasnt sure
if they actually stood for something specific.

mick
 

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