Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a generic dictionary as function parameter?

Tags:

c#

generics

My Case(I get compile error because T undefined):

public void f1(Dictionary<string,T> d){


}

How Can I solve the problem(I cant pass Object i have to pass original var type)?

(I am using .net 4.5)

Thanks

like image 397
Ben Avatar asked Sep 06 '25 05:09

Ben


1 Answers

You need to add the type parameter T:

public void f1<T>(Dictionary<string,T> d){
}

If you want the dictionary to contain values of any type then your only option is to use a Dictionary<string, object> and cast as appropriate.

like image 106
Lee Avatar answered Sep 09 '25 00:09

Lee