Share via


컴파일러 오류 CS0165

업데이트: 2008년 7월

오류 메시지

할당되지 않은 'name' 지역 변수를 사용했습니다.
Use of unassigned local variable 'name'

C# 컴파일러에서는 초기화하지 않은 변수의 사용을 허용하지 않습니다. 초기화되지 않은 변수의 사용을 컴파일러에서 발견하면 CS0165가 발생합니다. 자세한 내용은 필드(C# 프로그래밍 가이드)를 참조하십시오. 실제 코드와 관계없이 컴파일러에서 할당되지 않은 변수를 사용할 수 있는 구문을 발견한 경우 이 오류가 발생합니다. 이 동작 덕분에 확정 할당을 위한 복잡한 규칙을 만들 필요가 없습니다.

이 오류가 발생한 경우

자세한 내용은 https://blogs.msdn.com/ericlippert/archive/2006/08/18/706398.aspx를 참조하십시오.

예제

다음 샘플에서는 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;
   }
}

Visual Studio 2008에서 다음 코드를 실행하면 CS0165가 발생하지만 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;
    }

}

이 오류는 재귀적 대리자 정의에서 발생하며 다음 두 문에서 대리자를 정의함으로써 방지할 수 있습니다.

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

변경 기록

날짜

변경 내용

이유

2008년 7월

재귀적 대리자에 대한 텍스트와 코드 예제를 추가했습니다.

콘텐츠 버그 수정