How to Get T argument in generic class and return another type in Interface
I want to use this code:
List<U> SelectAll<T>() where T : class;
the problem is I want to pass T type and get U type but c# has T keyword but does not have U keyword.how I can Pass T type ad get another Type in Interface declration>
thanks
Sorry I post Answer here but I want show you code: I'm writing this code:
开发者_JAVA百科interface IRepoitory<T> where T : class
{
T CreateInstance();
IEnumerable<U> SelectAll();
what can I write instead of "U"? when I Impelement interface and I have this code:
IEnumerable<TResult> SelectAll<TResult>();
In Class that implement the interface I cant Return Any class instead of TResult thanks
T
is not a keyword in C# - it's just a conventional name for a type parameter. If you want to specify multiple type parameters, you can do so very easily:
public static List<U> SelectAll<T, U>() where T : class
(It's not clear where the data's coming from or how T
will be used here, admittedly.)
Conventionally type parameters either are T or start with T, so you might want:
public static List<TResult> SelectAll<TResult>() where T : class
If you can clarify what you're trying to do, we may be able to help more.
public static List<TResult> SelectAll<T, TResult>(T inParam) where T: class
{
return new List<TResult>();
}
Is that you want?? If you don't know the type of result, you need to tell method compiler what type to return. This is done using SelectAll<T, TResult>
精彩评论