CS0165-ös fordítási hiba

A "name" nem hozzárendelt helyi változó használata

A C#-fordító nem engedélyezi az nem inicializált változók használatát. Ha a fordító olyan változó használatát észleli, amely esetleg nem inicializálódott, cs0165-ös fordítási hibát generál. További információ: Mezők. Ez a hiba akkor jön létre, ha a fordító olyan szerkezettel találkozik, amely hozzárendeletlen változó használatát eredményezheti, még akkor is, ha az adott kód nem. Ez elkerüli a túl összetett szabályok szükségességét a határozott hozzárendeléshez.

További információ: Miért okoz egy rekurzív lambda határozott hozzárendelési hibát?

1. példa

Az alábbi minta a CS0165-öt hozza létre:

// CS0165.cs  
using System;  
  
class MyClass  
{  
    public int i;  
}  
  
class MyClass2  
{  
    public static void Main(string[] args)  
    {  
        // i and j are not initialized.  
        int i, j;  
  
        // You can provide a value for args[0] in the 'Command line arguments'  
        // text box on the Debug tab of the project Properties window.  
        if (args[0] == "test")  
        {  
            i = 0;  
        }  
        // If the following else clause is absent, i might not be  
        // initialized.  
        //else  
        //{  
        //    i = 1;  
        //}  
  
        // Because i might not have been initialized, the following
        // line causes CS0165.  
        j = i;  
  
        // To resolve the error, uncomment the else clause of the previous  
        // if statement, or initialize i when you declare it.  
  
        // The following example causes CS0165 because myInstance is  
        // declared but not instantiated.  
        MyClass myInstance;  
        // The following line causes the error.  
        myInstance.i = 0;
  
        // To resolve the error, replace the previous declaration with  
        // the following line.  
        //MyClass myInstance = new MyClass();  
    }  
}  

2. példa

A CS0165 fordítóhiba rekurzív delegáltdelegálásokban fordulhat elő. A hibát elkerülheti, ha két utasításban definiálja a delegáltat, hogy a változó ne legyen használatban az inicializálás előtt. Az alábbi példa a hibát és a megoldást mutatja be.

class Program  
{  
    delegate void Del();  
    static void Main(string[] args)  
    {  
        // The following line causes CS0165 because variable d is used
        // as an argument before it has been initialized.  
        Del d = delegate() { System.Console.WriteLine(d); };
  
        //// To resolve the error, initialize d in a separate statement.  
        //Del d = null;  
        //// After d is initialized, you can use it as an argument.  
        //d = delegate() { System.Console.WriteLine(d); };  
        //d();  
    }  
}  

Load(String)