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

后端 未结 4 1584
一整个雨季
一整个雨季 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 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

提交回复
热议问题