0

According to documentations, Enumerable.Range(1,10) should return IEnumerable<int> but IsGenericType property returns "false"! Why?!

    var temp = Enumerable.Range(1, 8);
    MessageBox.Show(temp.GetType().IsGenericType.ToString());

So, my question: I want to determine whether an object is IEnumerable<T> or not. but how can i do that with Enumarable.Range() output ? (IsArray property also gives "false")

Secondary question: How can I find its argument type (int in this example)?

Thanks

  • Does this answer your question? [getting type T from IEnumerable](https://stackoverflow.com/questions/906499/getting-type-t-from-ienumerablet) – 41686d6564 stands w. Palestine Jan 17 '22 at 08:55
  • @41686d6564 No, because IsGenericType property returns false! (i don't know why). so "GetGenericArguments()[0]" not works – user11448245 Jan 17 '22 at 08:57
  • 3
    `GetType()` returns the *runtime* type of the object, not the *compile-time* type. The compile-time type of `temp` is `IEnumerable`, but that's actually implemented by an internal class called [`RangeIterator`](https://source.dot.net/#System.Linq/System/Linq/Range.cs,635de868c5dbb1f5), which isn't generic. If you're interested in inspecting `IEnumerable`, use `typeof(IEnumerable).IsGenericType` etc – canton7 Jan 17 '22 at 08:57
  • 1
    That's because you get back runtime type of the inspected instance. This is a `RangeIterator` and if you would ask `temp.GetType().GetInterfaces()` you would find the generic interface. – Oliver Jan 17 '22 at 08:57
  • @user11448245 Read the _"Edit: I believe this will address the concerns in the comments"_ part of the accepted answer as well as [this answer](https://stackoverflow.com/a/906538/8967612). – 41686d6564 stands w. Palestine Jan 17 '22 at 08:58
  • @41686d6564 Let me clarify my question: I want to determine whether an object is IEnumerable or not. but how can i do that with Enumarable.Range() output ? "IsGenericType" property not works (why?) – user11448245 Jan 17 '22 at 09:03
  • 2
    I explained why -- the implementation of `Enumerable.Range` looks like [this](https://source.dot.net/#System.Linq/System/Linq/Range.cs,fda9d378095a6464): `return new RangeIterator(start, count)`. As you can see, `RangeIterator` is not generic. Your `temp.GetType()` method gets `typeof(RangeIterator)`. If you want to find out whether `RangeIterator` implements `IEnumerable`, and what that `T` is, see the answer which @41686d6564 linked – canton7 Jan 17 '22 at 09:05
  • 1
    @user11448245 The "why" question has been answered by two users in the comments. As to the other question, there you go: `temp.GetType().GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))`. This is a slightly modified version of the code in the answers to the duplicate. That's why you should read the answers and try to understand them before you can determine whether or not they solve your problem. – 41686d6564 stands w. Palestine Jan 17 '22 at 09:06
  • 3
    As another example of this happening: do you think `string` is a generic type? I don't. And yet it implements `IEnumerable`... – Jon Skeet Jan 17 '22 at 09:08
  • Perhaps you just need `if(temp is IEnumerable)` – Charlieface Jan 17 '22 at 21:27

0 Answers0