How to understand C# warning IDE0059: "Unecessary assignment"

Coreysan 1,651 Reputation points
2022-04-14T16:04:22.197+00:00

I have the following scenario:

public void testmylife (ref string a)
{
string wstr = string.empty;

if (a == 1)
{
wstr = "Drop dead";
}

if (a == 2)
{
wstr = "Stay alive";
}
}

I get IDE0059. How do I avoid that warning?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 56,931 Reputation points
    2022-04-14T18:04:07.407+00:00

    as there is no code that uses the value wstr is set to, there is no point in setting a value to it. if you removed the unneeded assignments, you would get a warning about an unused variable.

    your code snippet could be simplified to:

    public void testmylife (ref string a) 
    {
    }
    

    and function the same:


0 additional answers

Sort by: Most helpful