Sdílet prostřednictvím


CS0165 chyba kompilátoru

Použití nepřiřazené lokální proměnné "název"

Kompilátor jazyka C# nepovoluje použití neinicializovaných proměnných. Pokud kompilátor zjistí využívání proměnné, která pravděpodobně nebyla inicializována, generuje CS0165. Další informace naleznete v tématu Fields (C# Programming Guide). Všimněte si, že tato chyba je generována pokud kompilátor narazí na konstrukci, která může vyústit v použití nepřiřazené proměnná i v případě, že váš konkrétní kód proměnou nepoužívá. Tím je zabráněno potřebnosti příliš komplexních pravidel pro jednoznačné přiřazení.

Další informace naleznete v https://blogs.msdn.com/ericlippert/archive/2006/08/18/706398.aspx.

Příklad

Následující ukázka generuje 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;
   }
}

Následující kód generuje CS0165 v aplikaci Visual Studio 2008, ale ne v aplikaci 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;
    }

}

Tato chyba se objevuje při rekurzivním definování delegátů a lze se jí vyhnout definováním delegáta ve dvou příkazech:

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();
        }
    }