I am trying to test using 'as' keyword but get the null even though the variable I'm trying to test is a collection.
DoSomething(() => someMethodReturningCollection());
DoSomething(() => anotherMethodReturningAnObject());
public void DoSomething(Func<T> aFunc)
{
var result = aFunc();
var test = result as List<T>;
if(test != null){
DoTaskA();
return;
}
//Here `result` is not a collection
DoTaskB();
}
The test is always null. The typeof(T) shows as IEnumerable<T> otherwise.
I don't think this is a duplicate question
I'm trying to test if a type is a collection or not by using the 'as' operator. The issue seems to be the List<T> v. List<string> or List<customer>. I can successfully test result as List<customer> but not result as List<T>. It seems the as operator expects an explicit type - not a T.