Debugging some code I came across an "is IEnumerable" comparison, which confusingly evaluates to false in code but true in the Immediate Window.
I wonder if anyone can shed light to why this would happen?
Example:
public enum Fruit
{
Apples,
Strawberries
}
public void SomeMethod()
{
object myObj = new Fruit[] { Fruit.Apples, Fruit.Strawberries };
bool isListOfEnums = myObj is IEnumerable<Fruit>; // True
isListOfEnums = myObj is IEnumerable<Enum>; // False in code, but True in Immediate Window when debugged
}
(Immediate Window)
? myObj is IEnumerable<Enum>
true
It's a quirk of the intermediate window, basically. There are some pieces of code which evaluate differently there - it's one reason I generally prefer not to use the intermediate window.
An IEnumerable<Fruit>
isn't an IEnumerable<Enum>
. Each element of the latter would be a reference, as Enum
is a reference type (just like ValueType
is - I know, it's confusing) whereas each element of an IEnumerable<Fruit>
is a Fruit
value (not a reference).
See more on this question at Stackoverflow