How to determine if object is of type IEnumerable <T>?
Code:
namespace NS {
class Program {
static IEnumerable<int> GetInts() {
yield return 1;
}
static void Main() {
var i = GetInts();
var type = i.GetType();
Console.WriteLine(type.ToString());
}
}
}
Output:
NS.1.Program+<GetInts>d__0
If I change GetInts to return IList, everything is OK the output is:
System.Collections.Generic.List`1[System.Int32]
And this returns false:
namespace NS {
class Program {
static IEnumerable<int> GetInts() {
yield return 1;
}
static void Main() {
var i = GetInts();
var type = i.GetType();
Console.WriteLine(type.Equals(typeof(IEnumerable<int>)));
}
}
}