Variable is assigned but its value is never used (C#)

跟風遠走 提交于 2019-12-17 17:16:54

问题


In Visual Studio I used the following code:

 private bool answer = true;
    Private Buttonclick()
   {
      if()
       {
        answer =false
        }
   }

Compiler gives me a warning that says "answer is assigned but its value is never used". How do I fix this?


回答1:


It means you have given answer a value, but not referenced it elsewhere. Meaning you are not using answer elsewhere in your program.

You fix it by referencing answer from another part of your program.




回答2:


If you want to disable warnings, the error list contains a button that can stop them being shown in the list.

Additionally, a warning is just a warning. They won't stop your program from compiling, or even running. They may, however, declare in advance runtime errors and the like, so don't ignore them completely.




回答3:


The reason the warning pops up is not because the variable is never assigned, but because it is in fact never used for any sort of comparison in the rest of the code.

Please go through this reference: http://weblog.west-wind.com/posts/2009/Feb/28/Variable-is-assigned-but-its-Value-is-never-used-Warning



来源:https://stackoverflow.com/questions/30658839/variable-is-assigned-but-its-value-is-never-used-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!