huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11920

Variable number of method parameters with different types for each of them

Edit:

func(params dynamic[] parameters)
{

}

lets it accept variable parameters with variable types. Ignorance is not bliss.

Question:

I need to write a method that takes n number of lists each of different types such as:

 List<Type1> list1 = .....;
 List<Type2> list2 = .....;
 List<TypeN_1> listN_1 = .....;
 List<TypeN> listN = .....;
 var result=func(list1,list2,listN);

but I couldn't manage with "params" keyword because it doesn't let inner functions know < T > of each list.

 public int func< ? ? >(? ? ? ?)
 {
      int result=0;
      ... get all lists and put them in some function:
      innerFunc(list1);
      // which is declared as innerFunc<T>(List<T> p){}
      return result;
 }

Upvotes: 2

Views: 100

Answers (2)

erisco
erisco

Reputation: 14329

Do you truly need such a function? Perhaps you can instead write a function which works on two lists at a time, producing a new list.

List<C> Combine<A,B,C>(List<A>, List<B>, Func<A,B,C>)

Then you can handle multiple lists.

Combine(Combine(Combine(a, b, f1), c, f2), d, f3);
Combine(a, Combine(b, Combine(c, d, f1), f2), f3);

Without more context I cannot say whether this is possible for your problem.

Upvotes: 2

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

The greatest common denominator of lists with different generic type parameters is IList

int func(params IList[] parameters)
{
}

You can then get the generic type parameter with

Type t = parameters[i].GetType();
if (t.IsGenericType)
{
    Type typeArgument = t.GetGenericArguments()[0];
    ...

See: Type.GetGenericArguments Method ()

Upvotes: 1

Related Questions