This probably has a simple explanation that I don't see, but why the following law is legal:
public struct Foo { const object nullObject = null; public override string ToString() { if (nullObject == null) { return base.ToString(); } } }
While the following,
public struct Foo { const dynamic nullObject = null; public override string ToString() { if (nullObject == null) { return base.ToString(); } } }
gives the following compile-time error: Foo.ToString () ': do not all code paths return a value ?
Why does the fact that nullObject is dynamic prevent the compiler from asserting that nullObject will always be null ?
EDIT . Turning around the question and based on smoore's answer, why does the compiler allow dynamic const fields? Start with? Isn't that self-destruction? I know that this scenario has no real application, and it is frankly completely pointless, but I stumbled upon it just by accident and just became curious.
source share