Why is no warning given for this unused variable?

前端 未结 7 559
-上瘾入骨i
-上瘾入骨i 2020-12-06 16:25

When compiling the following program in VS2010, VS2008 or MonoDevelop on Windows, I get warning CS0219, \"The variable \'y\' is assigned but its value is never used\".

7条回答
  •  被撕碎了的回忆
    2020-12-06 16:43

    I could be off here, but I think it's because y is only set, whereas x is instantiated to something non-trivial - the instantiation could involve separate actions in the New() method, and since instantiating the variable could have side-effects, it's not considered unused. In your case it's just a base object(), so there's no impact, but perhaps the compiler isn't smart enough to tell the difference.

    With y, on the other hand, there are no side-effects to the instantiation, so it's considered unused - the application's code path would be unchanged if it were removed entirely.

提交回复
热议问题