Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0165

Use of unassigned local variable 'var'

The C# compiler does not allow the use of uninitialized variables. If the compiler detects the use of a variable that might not have been initialized, it generates CS0165.

Use new to create an instance of an object or assign a value.

The following sample generates CS0165:

// CS0165.cs
using System;

class MyClass
{
   public int i;
}

class MyClass2
{
   public static void Main(string [] args)
   {
      int i, j;
      if (args[0] == "test")
      {
         i = 0;
      }

      /*
      // to resolve, either initialize the variables when declared
      // or provide for logic to initialize them, as follows:
      else
      {
         i = 1;
      }
      */

      j = i;   // CS0165, i might be uninitialized

      MyClass x;
      x.i = 0;   // CS0165
      // use new as follows
      // MyClass x = new MyClass();
      // x.i = 0;
   }
}
Show:
© 2014 Microsoft