Code for illustration:
public struct MyStruct { public int SomeNumber; } public string DoSomethingWithMyStruct(MyStruct s) { if (s == null) return "this can't happen"; else return "ok"; } private string DoSomethingWithDateTime(DateTime s) { if (s == null) return "this can't happen";
Now “DoSomethingWithStruct” cannot be compiled with: “Operator” == “cannot be applied to operands of type“ MyStruct "and" <null> ". This makes sense, since it makes no sense to try to map the link to a structure that is a value type.
OTOH, "DoSomethingWithDateTime" compiles, but with a compiler warning: "Inaccessible code detected" in a line labeled "XX". Now I assume that there is no compiler error, because the DateTime structure overloads the "==" operator. But how does the compiler know that the code is unreachable? For example, does it look inside the code that overloads the "==" operator? (This uses Visual Studio 2005, if that matters).
Note: I am more curious than any of the above. Usually I do not try to use "==" to compare structures and zeros.
EDIT: I will try to simplify my question - why compiling "DoSomethingWithDateTime" when "DoSomethingWithMyStruct" does not. Both arguments are structures.
Moe sisko
source share