Michael
Michael

Reputation: 13636

How to create method that receive and return generic type?

How to create method that receive and return generic type?

I have to pass two different types to the same method.

  public List<Type1> param1 = new List<Type1>(); 
  public List<Type2> param2 = new List<Type2>();      

   List<Type3> var1 = MergeFoundSigns(param1);

   List<Type4> var2 = MergeFoundSigns(param2); 

  public Type MergeFoundSigns(List<T> param)
  {
     "some logic"
  }

Any idea how can I implemet the method above? Thank you in advance!

Upvotes: 0

Views: 155

Answers (2)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137547

If you know what the types will be:

public List<Type1> param1 = new List<Type1>(); 
public List<Type2> param2 = new List<Type2>();      

List<Type3> var1 = MergeFoundSigns<Type1, Type3>(param1);

List<Type4> var2 = MergeFoundSigns<Type2, Type4>(param2); 

public List<TOut> MergeFoundSigns<TIn, TOut>(List<TIn> param)
{
    // some logic
}

Upvotes: 2

hattenn
hattenn

Reputation: 4399

Try:

public List<T> MergeFoundSigns<T>(List<T> param)
{
     // ...
}

Upvotes: 1

Related Questions