0

Would like some code a bit like this: Func<T>< List<T>,T > myFunc = (inputList) => inputList.First();

I can define a Func without generic type but not sure how to do it generically, i.e add the 'T's.Is it possible?

Andrew
  • 547
  • 4
  • 13
  • 2
    You can declare such Func inside a class or function which is already generic. `public class MyClass{ Func, T> myFunc = (inputList) => inputList.First(); }` – Chetan Jan 23 '23 at 17:56
  • Thanks Chetan, good idea, ive updated the question slightly to try to make it clearer that isnt what im after in this case. – Andrew Jan 23 '23 at 18:02
  • If you know the element type of the list, you can specify it directly: `Func, string> myFunc = (inputList) => inputList.First();` – Olivier Jacot-Descombes Jan 23 '23 at 18:14
  • No idea what you want to achieve... Can you please [edit] post to clarify? Also what is exact errors you are facing? Normally it is not a problem to define generic types, presumably you are doing it inside a generic method/class already... You may also want to clarify bigger goal. – Alexei Levenkov Jan 23 '23 at 18:23
  • I've been unable to solve, but in my case a local method works just as well ```T genericLocalMethod(List paramList) { return paramList.First(); }``` – Andrew Jan 23 '23 at 18:25

0 Answers0