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:

it call .ctor last statement , why cannot have instance 2 first statements zeroed fields?
why doesn't c# compiler call default implicit parameterless
.ctorinstead ofintobjnull 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
Post a Comment