2

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; } 
5
  • 10
    what are you trying to achieve? Your problem might have a much better solution if you provide us with those details Commented Feb 21, 2016 at 19:58
  • Do the types have a common base type? If not the best you can do is use params object[] or params IEnumerable<object>[]. How are you supposed to know the type of a particular parameter in the function? Commented Feb 21, 2016 at 19:59
  • classes derived from strings and ints as fields Commented Feb 21, 2016 at 20:01
  • Inner functions need to have info about what is their base types (not common) Commented Feb 21, 2016 at 20:27
  • "params dynamic[] parameters" works. Problem solved. My ignorance. Commented Feb 21, 2016 at 20:44

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is possible but I needed as simple as a single function instruction to try my sql accelerator using gpgpu. Takes simple table rows and outputs the count of distinct patients who have taken medicine at least once for example. It computes relations between n tables just like left join n times with a count(id).
1

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 ()

2 Comments

All Lists become IList in the end? Even when I do (an array).ToList(); ?
All List<T> and all arrays implement the interface IList. This means that they are ILists. public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>. See List<T> Class

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.