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

后端 未结 4 1574
一整个雨季
一整个雨季 2020-12-07 06:18

In Visual Studio I used the following code:

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


        
相关标签:
4条回答
  • 2020-12-07 06:58

    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.

    0 讨论(0)
  • 2020-12-07 07:12

    In addition to the answers above, for more deep understanding of the this warning:

    This warning will not always pop-up (if you not use assigned variable). Assigning a non-constant expression or method result will NOT generate the warning. It is done intentionally, since such an unassigned variables can be used in debugging.

    For example:

     var answer = SomeObject.SomePropery; //will not generate CS0219
    

    There are excellent explanation in Microsoft.Docs: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0219

    0 讨论(0)
  • 2020-12-07 07:15

    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

    0 讨论(0)
  • 2020-12-07 07:15

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

    enter image description here

    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.

    0 讨论(0)
提交回复
热议问题