Grax Coding

Wednesday, November 5, 2014

Use of unassigned local variable in C#

Here is a simple tip that you may not have run across yet.

TLDR: Initialize a variable to null to indicate to the compiler that you plan to assign it later.

Suppose you have a situation where you need to create a variable but you will be assigning a value to it within a conditional statement (if, for, foreach, while, etc).  In the code that follows the conditional section you then want to reference the variable.

string myvar;
if (condtion)
{
     myvar = "Information";
}

Console.WriteLine(myvar);

But this code generates the error "Use of unassigned local variable 'myvar'"

However, if you initialize the variable to null, the compiler sees is as an assigned variable and compiles correctly.

string myvar = null;
if (condtion)
{
     myvar = "Information";
}

Console.WriteLine(myvar);

This code works.  Be aware that now you are responsible for assigning a value or you will get a null reference exception.  To be on the safer side, I only initialize a variable to null when I have reviewed the following code and I am satisfied that I will not get a null reference exception.