산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C# 변수 범위

C# 변수 범위

이 자습서에서는 예제를 통해 C#의 변수 범위에 대해 알아봅니다.

변수 범위는 코드의 특정 부분에서 변수의 가용성을 나타냅니다.

C#에서 변수에는 세 가지 유형의 범위가 있습니다.

<시간>

C# 클래스 수준 변수 범위

C#에서는 클래스 내에서 변수를 선언할 때 클래스 내에서 변수에 액세스할 수 있습니다. 이를 클래스 수준 변수 범위라고 합니다. .

클래스 수준 변수는 필드라고 하며 클래스의 메서드, 생성자 및 블록 외부에서 선언됩니다. 예를 들어,

using System;
namespace VariableScope {
  class Program {

    // class level variable
    string str = "Class Level";

    public void display() {
      Console.WriteLine(str);
    }

    static void Main(string[] args) {
      Program ps = new Program();
      ps.display();

      Console.ReadLine();
    }
  }
}

출력

Class Level

위의 예에서 str이라는 변수를 초기화했습니다. 프로그램 내부 수업.

클래스 수준 변수이므로 클래스 내부에 있는 메서드에서 액세스할 수 있습니다.

public void display() {
  Console.WriteLine(str);
}

클래스 수준 변수는 클래스 전체에서 액세스할 수 있기 때문입니다.

참고 :정적 메서드를 통해 클래스 수준 변수에 액세스할 수 없습니다. 예를 들어 Program 내부에 정적 메서드가 있다고 가정합니다. 수업.

static void display2() {

  // Access class level variable
  // Cause an Error
  Console.WriteLine(str);
}
<시간>

방법 수준 변수 범위

메소드 내부에서 변수를 선언하면 메소드 외부에서 변수에 접근할 수 없습니다. 이를 메서드 수준 변수 범위라고 합니다. . 예를 들어,

using System;

namespace VariableScope {
  class Program {

    public void method1() {
      // display variable inside method
      string str = "method level";
    }

    public void method2() {

      // accessing str from method2()
      Console.WriteLine(str);
    }

    static void Main(string[] args) {
      Program ps = new Program();
      ps.method2();

      Console.ReadLine();
    }
  }
}

위의 예에서 str이라는 변수를 생성했습니다. method1() 내부 .

// Inside method1()
string str = "method level";

여기서 str 메서드 수준 변수입니다. 따라서 method1() 외부에서는 액세스할 수 없습니다. .

그러나 str에 액세스하려고 하면 method2()의 변수

// Inside method2
Console.WriteLine(str);  // Error code

오류가 발생합니다.

Error   CS0103     The name 'str' does not exist in the current context  

이는 메서드 수준 변수가 생성된 메서드 내부에 범위가 있기 때문입니다. 예를 들어,

using System;
namespace VariableScope {
  class Program {

    public void display() {
     string str = "inside method";

      // accessing method level variable
      Console.WriteLine(str);
    }

    static void Main(string[] args) {
    Program ps = new Program();
    ps.display();

    Console.ReadLine();
    }
  }
}

출력

inside method

여기에서 str을 만들었습니다. 변수를 만들고 동일한 메소드 display() 내에서 액세스했습니다. . 따라서 코드는 오류 없이 실행됩니다.

<시간>

C#의 블록 수준 변수 범위

블록 내에서 변수를 선언할 때(for 루프, while 루프, if..else) 변수는 블록 내에서만 액세스할 수 있습니다. 이를 블록 수준 변수 범위라고 합니다. . 예를 들어,

using System;

namespace VariableScope {
  class Program {
    public void display() {

      for(int i=0;i<=3;i++) {
        	 
      }
    Console.WriteLine(i);
    }

    static void Main(string[] args) {
      Program ps = new Program();
      ps.display();

      Console.ReadLine();
    }
  }
}

위 프로그램에서 블록 레벨 변수 i을 초기화했습니다. for 내부 루프.

for(int i=0;i<=3;i++) {
       	 
}

i 이후 for 외부의 변수에 액세스하려고 할 때 블록 수준 변수입니다. 루프,

// Outside for loop
Console.WriteLine(i);

오류가 발생합니다.

Error	 CS0103  The name 'i' does not exist in the current context

C 언어

  1. 파이썬 전역 키워드
  2. 예제가 있는 C++ 포인터
  3. C 변수, 데이터 유형, 상수
  4. Verilog 할당
  5. 게이트 레벨 모델링
  6. Verilog 타임스케일 범위
  7. 자바 - 변수 유형
  8. C - 변수
  9. C - 범위 규칙
  10. C - 포인터