Errore del compilatore CS0165

Utilizzo della variabile locale 'nome' non assegnata

Il compilatore C# non consente l'utilizzo di variabili non inizializzate. Se rileva la presenza di una variabile che può non essere stata inizializzata, genera l'errore CS0165. Per ulteriori informazioni, vedere Campi (Guida per programmatori C#). Notare che questo errore viene generato quando il compilatore rileva un costrutto che potrebbe comportare l'utilizzo di una variabile non assegnata, anche se il codice specifico non lo prevede. In questo modo si evita la necessità di regole eccessivamente complesse per un'assegnazione definita.

Per ulteriori informazioni, vedere https://blogs.msdn.com/ericlippert/archive/2006/08/18/706398.aspx (informazioni in lingua inglese).

Esempio

Il seguente codice di esempio genera l'errore 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 myClass;
      myClass.i = 0;   // CS0165
      // use new as follows
      // MyClass myClass = new MyClass();
      // myClass.i = 0;
   }
}

Il codice seguente comporta la generazione dell'errore CS0165 in Visual Studio 2008, ma non in Visual Studio 2005:

//cs0165_2.cs
class Program
{
    public static int Main()
    {
        int i1, i2, i3, i4, i5;

        // this is an error, because 'as' is an operator
       // that is not permitted in a constant expression.
        if (null as object == null)
            i1 = 1;

        // this is an error, because 'is' is an operator that
        //  is not permitted in a constant expression.
        // warning CS0184: The given expression is never of the provided ('object') type
        if (!(null is object))
            i2 = 1;

        // this is an error, because a variable j3 is not
        // permitted in a constant expression.
        int j3 = 0;
        if ((0 == j3 * 0) && (0 == 0 * j3))
            i3 = 1;

        // this is an error, because a variable j4 is not
        // permitted in a constant expression.
        int j4 = 0;
        if ((0 == (j4 & 0)) && (0 == (0 & j4)))
            i4 = 1;

        // this might be an error, because a variable j5 is not
        // permitted in a constant expression.
        // warning CS1718: Comparison made to same variable; did you mean to compare something else?
        int? j5 = 1;
        if (j5 == j5)
            i5 = 1;


        System.Console.WriteLine("{0}{1}{2}{3}{4}{5}", i1, i2, i3, i4, i5); //CS0165

        return 1;
    }

}

Questo errore si verifica nelle definizioni di delegati ricorsivi e può essere evitato definendo il delegato in due istruzioni:

class Program
    {
        delegate void Del();
        static void Main(string[] args)
        {
            Del d = delegate() { System.Console.WriteLine(d); }; //CS0165
// Try this instead:
// Del d = null;
//d = delegate() { System.Console.WriteLine(d); };
            d();
        }
    }