0

I have the following chunk of code which is using the ?? Operator for initialization:

    private string _name;
    public string Name
    {
        get { return _name ?? string.Empty; }
    }

I guess that this code is valid since it works, however I get the following warning in my error list:

'_name' is never assigned to, and will always have its default value null

Is there a way to make Visual Studio stop warning about this?

leppie
  • 115,091
  • 17
  • 196
  • 297
Yoav
  • 3,326
  • 3
  • 32
  • 73
  • There might be an option in VS to disable the warning, but you could also do `private string _name = "";`. – Tim Nov 02 '14 at 10:10
  • 1
    The `??` operator never initialises anything, and the documentation you link to does not claim it does. –  Nov 02 '14 at 10:17

2 Answers2

5

Is there a way to make Visual Studio stop warning about this?

The best way would be to understand the warning and fix the bug that it indicates.

If you never assign to _name this means that reading from _name will always return null. Is that really what you intend?

You can also suppress this warning.

Also note, that ?? simply computes a new value from its operands. It does not assign a new value to anything.

Community
  • 1
  • 1
usr
  • 168,620
  • 35
  • 240
  • 369
  • What bug? if I'm not mistaken this warning has 2 part in it: 1) `'_name' is never assigned to` which is incorrect since there can't be a case where it won't be assigned to. 2) `and will always have its default value null` also not true for the same reasons I mentioned above – Yoav Nov 02 '14 at 10:17
  • Where are you assigning to it? Not in the code shown. Maybe you assume that `??` writes to `_name` since you call that "*initialization*"? That is not so. I should have picked up on that word immediately and corrected it. – usr Nov 02 '14 at 10:19
  • In the code snippet you posted, `_name` most definitely is never assigned to. Your property is a read only property that returns the value of `_name` or null if it is null. The getter doesn't assign anything. – Tim Nov 02 '14 at 10:19
  • @Yoav Simply inspect the value of `_name` after reading the `Name` property to verify that `??` does not change it. –  Nov 02 '14 at 10:19
  • 1
    +1 for this: "The best way would be to understand the warning and fix the bug that it indicates.". Warnings are there for a reason. – Tim Nov 02 '14 at 10:20
  • While in this specific case he might be doing it incorrectly, as a matter of fact, Visual Studio brings up this warning if you don't assign a value to the variable in the "sequential" code. For example, if you declare a variable without assigning a value, and then use an if-else block and assign some value to it in both `if` and `else` bodies, VS will still bring up this warning. This is unlike the case of "Function doesn't return a value" warning, which check all execution paths. – dotNET Nov 02 '14 at 10:28
  • @dotNET not in my testing: `string s; if (Environment.TickCount == 0) s = ""; else s = "s"; Console.WriteLine(s);` – usr Nov 02 '14 at 10:32
  • @dotNET Not only that, the warning you're talking about is a different warning from the one the OP is getting. –  Nov 02 '14 at 10:47
3

Init _name explicitly:

 private string _name = null; 

Or

private string _name = String.Empty; 

And then you won't have any warning....

TamarG
  • 3,522
  • 12
  • 44
  • 74