What does "Use of unassigned local variable" mean? [duplicate]
I keep getting this error for annualRate, monthlyCharge, and lateFee.
11 Answers 11
The compiler isn’t smart enough to know that at least one of your if blocks will be executed. Therefore, it doesn’t see that variables like annualRate will be assigned no matter what. Here’s how you can make the compiler understand:
The compiler knows that with an if/else block, one of the blocks is guaranteed to be executed, and therefore if you’re assigning the variable in all of the blocks, it won’t give the compiler error.
By the way, you can also use a switch statement instead of if s to maybe make your code cleaner.
Change your declarations to this:
The error is caused because there is at least one path through your code where these variables end up not getting set to anything.
Because if none of the if statements evaluate to true then the local variable will be unassigned. Throw an else statement in there and assign some values to those variables in case the if statements don’t evaluate to true. Post back here if that doesn’t make the error go away.
Your other option is to initialize the variables to some default value when you declare them at the beginning of your code.
Give them a default value:
Basically, all possible paths don’t initialize these variables.
Use the keyword "default".
![]()
There are many paths through your code whereby your variables are not initialized, which is why the compiler complains.
Specifically, you are not validating the user input for creditPlan — if the user enters a value of anything else than «0»,»1″,»2″ or «3» , then none of the branches indicated will be executed (and creditPlan will not be defaulted to zero as per your user prompt).
As others have mentioned, the compiler error can be avoided by either a default initialization of all derived variables before the branches are checked, OR ensuring that at least one of the branches is executed (viz, mutual exclusivity of the branches, with a fall through else statement).
How to resolve C# error: Use of unassigned local variable
This error occurs you declared a local varible, however you don’t initialize it. For Example:
Now you will get the Compile Time Error: «Use of Unassigned Local Variable num». Local variables have to be assigned before they can be used.
So you need initialize the local variable first, and then use it like:
In C#, a value type has default value, it works for class members and array element, but not local variables. Local variables are not classified as initially assigned, they must be classified as «definitely assigned» with target vaue when they are been used.
Fot the variables are classified as being automatically «initially assigned» to their default value, you can use it before they own an «definitely» value.
Use of unassigned local variable c что значит
else if (radioButton15.Checked)
varResults = Question1 + Question2 + Question3 + Question4 + Question5;
if (varResults == 5)
MessageBox .Show( "You are most like Zombie #12: On TV as in life, you’re a bit player and you get lost in a crowd of one." );
else if (varResults == 6)
MessageBox .Show( "You are most like Rick: Emo much? Seriously, you need to lighten up! People follow your lead but they’re groaning inside. And you’re a little bit cuckoo. I suggest Lithium." );
else if (varResults == 7)
MessageBox .Show( "You are most like Lori: Your theme song is \"Torn Between Two Lovers.\" You want it all except when it comes to food. You need to eat a sammich, girl! And roll down your damned pants! No one likes you and we’re all glad you died." );
else if (varResults == 8)
MessageBox .Show( "You are most like Andrea: You’re a rebel with low self-esteem. Maybe you’re a rebel BECAUSE you have low self-esteem. Frankly, no one cares why. No one listens to you unless you fart." );
else if (varResults == 9)
MessageBox .Show( "You are most like Carl: Most people find you annoying and try to avoid your company. You’re handy at reaching things on the bottom shelf because you’re short, but that’s pretty much the extent of your usefulness. People call you an ankle-biter behind your back." );
else if (varResults == 10)
MessageBox .Show( "You are most like Daryl: You’re kind of smelly and sweaty and people are usually repulsed by you at first meeting, but you are actually like whatever they call those ugly rocks that have pretty crystals inside." );
else if (varResults == 11)
MessageBox .Show( "You are most like Carol: You’re too often overlooked unless it concerns girl-stuff, but you’re willing and able to step up to the plate….and wash it." );
else if (varResults == 12)
MessageBox .Show( "You are most like Maggie: In any crowd, you’re usually considered the prettiest (even if only due to lack of competition), but you’re not just another pretty face. You’re good at kicking butt too. You suck." );
else if (varResults == 13)
MessageBox .Show( "You are most like Beth: When your name is mentioned, it most often prompts the question \"Who?\" You may be blonde but you’re not having more fun. There’s really not much more to say about you." );
else if (varResults == 14)
MessageBox .Show( "You are most like Michonne: You one bad-ass girl. You don’t take any crap, preferring to lop off heads when anyone opposes you. Brava!" );
else if (varResults == 15)
MessageBox .Show( "You are most like The Governor: I don’t even know what to say about YOU! Naughty!" );
C# Error CS0165 – Use of unassigned local variable ‘name’
You will receive this error when the C# compiler detects that you have used an uninitialized variables with-in your program.
For example, the below code snippet results in the error code CS0165 because the variable val is not initialized.

Error CS0165 Use of unassigned local variable ‘val’ ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 10 Active
Solution
C# doesnot allow you to use uninitialized variables. You will need to initialize the variable to a default value before you want to use them in your C# code.