c# - Initializing a Nullable Type with Initobj Opcode in IL -


why doesn't c# compiler call default implicit parameterless .ctor instead of intobj null assigned nullable value types?

lets have such code:

nullable<int> ex1 = new nullable<int>(); nullable<int> ex2 = null; nullable<int> ex3 = new nullable<int>(10);  

the il output these 2 lines this:

enter image description here

it call .ctor last statement , why cannot have instance 2 first statements zeroed fields?

why doesn't c# compiler call default implicit parameterless .ctor instead of intobj null assigned nullable value types?

because nullable<t> (or other value type) doesn't have parameterless constructor. can verify looking @ in decompiler or using reflection (e.g. typeof(nullable<>).getconstructors()).

if write new t() value type t in c#, looks invokes parameterless constructor (and c# spec calls constructor too), that's not happens, because there no parameterless .ctor method on value types.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -