c# - Why can't I give a default value as optional parameter except null? -


i want have optional parameter , set default value determine, when this:

private void process(foo f = new foo()) {  } 

i'm getting following error (foo class):

'f' type of foo, default parameter of reference type other string can initialized null.

if change foo struct works default parameterless constructor.

i read documentation , it's states cannot doesn't mention why?, why restriction exists , why string excluded this? why value of optional parameter has compile-time constant? if wouldn't constant side-effects ?

a starting point clr has no support this. must implemented compiler. can see little test program:

class program {     static void main(string[] args) {         test();         test(42);     }     static void test(int value = 42) {     } } 

which decompiles to:

.method private hidebysig static void  main(string[] args) cil managed {   .entrypoint   // code size       15 (0xf)   .maxstack  8   il_0000:  ldc.i4.s   42   il_0002:  call       void program::test(int32)   il_0007:  ldc.i4.s   42   il_0009:  call       void program::test(int32)   il_000e:  ret } // end of method program::main  .method private hidebysig static void  test([opt] int32 'value') cil managed {   .param [1] = int32(0x0000002a)   // code size       1 (0x1)   .maxstack  8   il_0000:  ret } // end of method program::test 

note how there no difference whatsoever between 2 call statements after compiler done it. compiler applied default value , did @ call site.

also note still needs work when test() method lives in assembly. implies default value needs encoded in metadata. note how .param directive did this. cli spec (ecma-335) documents in section ii.15.4.1.4

this directive stores in metadata constant value associated method parameter number int32, see §ii.22.9. while cli requires value supplied parameter, tools can use presence of attribute indicate tool rather user intended supply value of parameter. unlike cil instructions, .param uses index 0 specify return value of method, index 1 specify first parameter of method, index 2 specify second parameter of method, , on.

[note: cli attaches no semantic whatsoever these values—it entirely compilers implement semantic wish (e.g., so-called default argument values). end note]

the quoted section ii.22.9 goes detail of constant value means. relevant part:

type shall 1 of: element_type_boolean, element_type_char, element_type_i1, element_type_u1, element_type_i2, element_type_u2, element_type_i4, element_type_u4, element_type_i8, element_type_u8, element_type_r4, element_type_r8, or element_type_string; or element_type_class value of zero

so that's buck stops, no way reference anonymous helper method kind of code hoisting trick cannot work either.

notable isn't problem, can implement arbitrary default value argument of reference type. example:

private void process(foo f = null) {     if (f == null) f = new foo();  } 

which quite reasonable. , kind of code want in method instead of call site.


Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -