Why can an IEnumerable = null be passed without error but an IList=Null will not compile?
4 回答
That's not true. You can set a variable of type IEnumerable or IList to a null reference and it will compile.
There is something else in your code that is wrong.
This works fine:
IList<int> Foo() {
return null;
}
It's hard to know exactly what you mean. You say a null reference for an IEnumerable can be 'passed without error'. 'Passing' is a runtime thing, but you say with an IList you get a compile error? That sounds confused. You'll need to give us some more info.
Well, with your exact question, it could be because C# is case-sensitive - null
is a literal, but Null
isn't. It's hard to say given that you haven't posted any code which could actually compile though.
If that's not the problem in your real code, post your real code and we'll see what we can do.