Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a generic List<> in C#

Tags:

c#

list

generics

I am going brain dead on this; I have several List' defined, based on specific classes (c1, c2, c3...). I have a method that will process information on these lists. What I want to do is pass in the specific list, but have the method accept the generic list, and then via typeof determine what specific work to do. I know its possible, but I cant seem to get the syntax right on the method side. so, for example:

List<c1> c1var; List<c2> c2var; List<c3> c3var;  some_method(c1var); some_method(c2var); some_method(c3var);  class some_thing some_method(List<> somevar) if typeof(somevar).name = x then esle if typeof(somevar).name = y then.... 

How do I set up the parameter list for the method?

thanks in advance R. Sanders

like image 871
RockySanders99 Avatar asked Jan 12 '10 17:01

RockySanders99


People also ask

How do you pass a generic List to a class?

The ListBox is using the ToString() method of the Generic List which returns a description of it (which is what you see) NOT it's contents. To get that you will have to write your own class which inherits from List<> and in that class write your own ToString() method which returns a string in the format you like.

What is the use of T in C#?

T is called type parameter, which can be used as a type of fields, properties, method parameters, return types, and delegates in the DataStore class. For example, Data is generic property because we have used a type parameter T as its type instead of the specific data type.

Can lists be generic?

You can't declare a list of generic types without knowing the generic type at compile time. You can declare a List<Field<int>> or a List<Field<double>> , but there is no other common base type for Field<int> and Field<double> than object .

What is a generic constraint?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.


2 Answers

You need to declare some_method to be generic, as well.

void SomeMethod<T>(List<T> someList) {     if (typeof(T) == typeof(c1))     {          // etc     } } 
like image 121
JSBձոգչ Avatar answered Sep 23 '22 10:09

JSBձոգչ


Careful with the use of typeof(typ1) == typeof(typ2). That will test to see if the types are equivalent disregarding the type hierarchy.

For example:

typeof(MemoryStream) == typeof(Stream); // evaluates to false new MemoryStream() is Stream; //evalutes to true 

A better way to check to see if an object is of a type is to use the 'is' keyword. An example is below:

public static void RunSnippet() {     List<c1> m1 = new List<c1>();     List<c2> m2 = new List<c2>();     List<c3> m3 = new List<c3>();      MyMeth(m1);     MyMeth(m2);     MyMeth(m3); }  public static void MyMeth<T>(List<T> a) {     if (a is List<c1>)     {         WL("c1");     }     else if (a is List<c2>)     {         WL("c2");     }     else if (a is List<c3>)     {         WL("c3");     } }    
like image 22
Brian Hasden Avatar answered Sep 23 '22 10:09

Brian Hasden