c# - When is a nullable not a nullable? -


trying understand default behavior of getvalueordefault(). if have nullable property should getvalueordefault() return underlying default value or instantiated value of null...because instantiated value (null) value don't need "default" value.

for example:

namespace nullable_test {    public class autopropnulltest    {      public int? autovalue { get; set; }    }     public class manualpropnulltest    {        private int? i;        public int? manualvalue       {                 {             return i;         }          set         {             = value;         }       }    } } 

the above instantiate null values calling valueordefault return zero.

        manualpropnulltest x = new manualpropnulltest();          autopropnulltest y = new autopropnulltest();          //x & y inspect null          int? q = x.manualvalue.getvalueordefault();          //q result in 0 

i understand splitting hairs type question trying reconcile think default should , reality is.

thank you

the nullable<t> type wraps value type in type compiler , run-time understand "nullable" type, in can treated in contexts if have null value.

but note null value means absence of value, not default value.

when call getvalueordefault(), you're asking non-nullable value nullable<t> value, or value type's own default value if nullable<t> value in fact "null".

in scenario, should expect underlying default value. that's method intended return. note in fact actual value, , if assigned yet nullable<t> variable, considered non-null (i.e. value present).


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -