Prismo
Prismo

Reputation: 11

Variable is assigned but its value is never used

The code:

int num01;
num01 = 20; 

Gives:

Warning CS0219 The variable 'num01' is assigned but its value is never used

Upvotes: 1

Views: 10946

Answers (3)

mbabramo
mbabramo

Reputation: 2843

CS0219 is a warning, not an error, so your program should compile. It might seem strange to you, because you are "using" the variable by assigning 20 to it. The warning message should be understood as pointing out that you are not using the variable in any other way besides assigning to it. In other words, the assignment cannot have any effect, other than if you are stepping through the program and looking at values in the debugger. If you add another line to your code using the variable in any way other than an assignment, then the warning will go away. For example, the following will do:

    int num01;
    num01 = 20;
    Console.WriteLine(num01);

Upvotes: 0

JChilton
JChilton

Reputation: 1

This is a warning, not an error. Everything will still compile and run fine.

Assigning a value to a variable is not 'using' it.

Although not recommended, you can suppress this warning by adding the following line above it

#pragma warning disable CS0219

Upvotes: 0

RSchneyer
RSchneyer

Reputation: 300

https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0219

You're creating a variable named num01, but you're not using it. I'm far from a C# expert, but I assume the easiest way to get rid of this warning would be to either use or remove the offending variable.

Upvotes: 5

Related Questions