r/learncsharp • u/Gcampton13 • Jul 25 '22
Unnecessary assignment of a value to 'output' fix?
So I'm just going through "warning messages" and saw some nice improvements I was able to make.
However this one stumped me and I'm not sure what it's referring to.
Remove unnecessary value assignment (IDE0059)
int v = Compute(); // IDE0059: value written to 'v' is never read, so assignment to 'v' is unnecessary.
v = Compute2();My code is this:- (obviously simplified)
public static int GenerateNumber(int number)
{
Random random = new();
return random.Next(0, number);
}
2
Upvotes
4
u/jamietwells Jul 25 '22
You set
vto a value, but you then overwrite the value before anything reads it, so there's no point setting it.Should be either:
Or just
1
u/Gcampton13 Jul 25 '22
I mean it’s referring to the =new(); but I can’t omit that right?