Why is it comparing a single object and an IEnumerable is valid for RefTypes?
Because it's entirely feasible that it will return true
:
class CunningFooRef : FooRef, IEnumerable<FooRef>
{
// Implementation...
}
FooRef fooRef = new CunningFooRef();
IEnumerable<FooRef> fooRefEnumerable = Enumerable.Empty<FooRef>();
Console.WriteLine(fooRef == fooRefEnumerable); // False
fooRefEnumerable = (IEnumerable<FooRef>) fooRef;
Console.WriteLine(fooRef == fooRefEnumerable); // True
Note that this is using the same compile-time types for fooRef
and fooRefEnumerable
as you're using.
If you seal FooRef
, so the compiler knows that it's impossible for a FooRef
reference to also be an IEnumerable<FooRef>
reference, then you'll get a compile-time error.