c# - How Does .Net Allow Nullables To Be Set To Null -


the nullable<t> type defined struct. in .net, can't assign null struct because structs value types cannot represented null (with exception of nullable<t>).

int = null; // won't compile - know int? = null; // compile, , i'm glad does, , should compile, why? 

how did nullable<t> become exception rule "you can't assign null value type?" decompiled code nullable<t> offers no insights of how happens.

how did nullable<t> become exception rule "you can't assign null value type?"

by changing language, basically. null literal went being "a null reference" "the null value of relevant type".

at execution time, "the null value" nullable value type value hasvalue property returns false. this:

int? x = null; 

is equivalent to:

int? x = new int?(); 

it's worth separating framework parts of nullable<t> language , clr aspects. in fact, clr doesn't need know nullable value types - far i'm aware, important aspect null value of nullable value type boxed null reference, , can unbox null reference null value of nullable value type. introduced before .net 2.0's final release.

the language support consists of:

  • syntactic sugar in form of ? int? equivalent nullable<int>
  • lifted operators
  • the changed meaning of null
  • the null-coalescing operator (??) - isn't restricted nullable value types

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -