c# - dynamic field const vs. any old reference field const behavior -


this has simple explanation i'm failing see, why following code legal:

    public struct foo     {         const object nullobject = null;          public override string tostring()         {             if (nullobject == null)             {                 return base.tostring();             }          }     } 

while following,

    public struct foo     {         const dynamic nullobject = null;          public override string tostring()         {             if (nullobject == null)             {                 return base.tostring();             }          }     } 

gives following compile time error: foo.tostring()': not code paths return value?

why fact nullobject being dynamic makes compiler not able assert nullobject null?

edit: expanding on question, , based on smoore's answer, why compiler allow dynamic const fields begin with? isn't kind of self defeating? know scenario has no real application @ , it's frankly quite pointless stumbled upon sheer accident , got curious.

because dynamic objects not resolved @ compile time, compiler has no idea null. dynamic object won't resolved until run time.

edit:

i see confusion, why allow const dynamic?

my guess dynamic changed non-nullable type, in case tostring not return value, that's guess. i'm thinking might still want have ability have constant dynamic, can ensure value won't change outside of static constructor, not know type until run-time.

another possibility, servy point out, it's such corner-case it's not worth fixing.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -