Tuesday, 15 June 2010

c# - Warning CS0219 on unused local variable depends on Nullable syntax? -



c# - Warning CS0219 on unused local variable depends on Nullable<> syntax? -

consider code:

static void main() { int? = new int?(12); // warning cs0219: variable 'a' assigned value never used int? b = 12; // warning cs0219: variable 'b' assigned value never used int? c = (int?)12; // (no warning 'c'?) }

the 3 variables a, b , c equivalent. in first one, phone call public instance constructor on nullable<> explicitly. in sec case, utilize implicit conversion t t?. , in 3rd case write conversion explicitly.

my question is, why visual c# 5.0 compiler (from vs2013) not emit warning c same way first 2 variables?

the il code produced same in 3 cases, both debug (no optimizations) , release (optimizations).

not sure if warning covered language specification. otherwise, "valid" c# compiler inconsistent this, wanted know reason is.

ps! if 1 prefers var keyword lot, plausible write var c = (int?)12; cast syntax needed create var work intended.

pps! aware no warning raised in cases int? neverused = methodcallthatmighthavesideeffects();, see another thread.

c# compilation nullable compiler-warnings

No comments:

Post a Comment