Wednesday 15 January 2014

c# - CS1720 warning erroneously(?) generated for extension method -



c# - CS1720 warning erroneously(?) generated for extension method -

consider next program:

static class programme { static void extension(this string str) { if(str == null) console.writeline("string null"); else console.writeline("string not null"); } static void main(string[] args) { default(string).extension(); // <--- warning extension(default(string)); // <--- no warning } }

the output expected:

string null string null

but, c# compiler gives cs1720 warning on first marked line:

warning cs1720: look cause system.nullreferenceexception because default value of 'string' null

my question is: why compiler suggest there nullreferenceexception? first phone call extension() equivalent sec one, sec not produce warning. both calls ought safe because this string str parameter, can safely null, seen in sec line. have been able reproduce on 3.5, 4.0, , 4.5 compilers, not mono 3.0.7.

obviously reply yes, warning incorrect. have proven quite well. (probably reason warning after all)

as discussed earlier doesn't harm phone call extension method null value. guess compiler team didn't through hussle check whether method phone call extension method or not.

c#

No comments:

Post a Comment