It is possible to create a Func object what references a generic method? like the LINQ OrderBy:
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If I understand you correctly, you’re asking if you can reference a generic method from within an anonymous method.
The answer is yes.
For example, suppose you want some
Functhat returns the elements of anIEnumerable<int>object in sorted order (precisely likeOrderBy<int, int>). You could do this:Then you could use this
Funcjust like any other:Output:
On the other hand, if you’re asking whether it’s possible to create a “generic anonymous method,” like this:
Then it depends on your context. This can be done from within a context where
Tis already declared as a generic type parameter — namely, within a generic class or generic method. (See Freddy Rios’s answer.) Outside of such a context, unfortunately, it is illegal.