Tried this code:
public static T NotEmpty<T, X> (T instance, string paramName = null) where T : IEnumerable<X>
{
if (instance != null)
if (instance.Any ())
return instance;
throw new Exception ();
}
static void Main (string[] args)
{
var list = new List<int> () { 1, 2, 3 };
var t = NotEmpty (list);
}
EDIT: So I want to pass any type that implements IEnumerable
like List<int>
and return instance of same type ( LIst<int>
).
error:
var t = NotEmpty (list);
The type arguments for methodNoName.Program.NotEmpty<T,X>(T, string)
cannot be inferred from the usage. Try specifying the type arguments explicitly.
I believe you don't need two generic parameter. You can replace T
as IEnumerable<T>
public static IEnumerable<T> NotEmpty<T> (IEnumerable<T> instance, string paramName = null)
{
if (instance != null && instance.Any())
return instance;
throw new Exception ();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.