Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a function like bellow.
C#
public void test(??)
        {
           ..........
           ..........
         /*Here I nedd call list ,which I send as parametar*/
  List<??> rangePlants = something;
           ..........
           ..........
            
        }

I am trying to send a list of any class of my application as parameter.suppose I have 3 class A,B,C sometimes I have to pass List , List or List<c>.can any body help me with a example?
Posted
Comments
BillWoodruff 9-Jan-15 8:29am    
You need to clarify if you are talking about passing a List of Class Instances, or a List of Class Types. If you are passing Instances, then do the Instances all inherit from a common Interface or a common Class ?

You can make use of Interface over here.
Create an interface say "I" and implement that interface in all the 3 classes A,B,C.

After that in the method Test you can accept "I" and in the list also you can use "I", like..
public void test(I interfaceImplementedObject)
        {
           ..........
           ..........
         /*Here I nedd call list ,which I send as parametar*/
  List<I> rangePlants = something;
           ..........
           ..........

        }
 
Share this answer
 
Comments
KuntalBose 9-Jan-15 11:56am    
can you please give a sample?that will help me for better understand.
You better make it generic:
C#
public void test<t>(rest of the parameter list)
        {
           ..........
           ..........
  List<t> rangePlants = something;
           ..........
           ..........           
        }
</t></t>

There might be situations where generic approach this is not applicable, like in case of Activator[^], in tat case, as you can see on the link you can pass Type typed parameter.
 
Share this answer
 
Comments
BillWoodruff 9-Jan-15 7:19am    
I'd like to see you explain why/how 'Activator becomes an issue here. thanks, Bill
Zoltán Zörgő 9-Jan-15 8:55am    
No, not an issue. In cases like the Activator.CreateInstance method, you can't use the generic approach, you need the type itself. But it depend on the actual action performed in the method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900