Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 클래스의 내부 변수 범위는 무엇입니까?

<시간/>

내부 변수는 내부 접근 지정자를 사용하여 설정됩니다.

internal double length;
internal double width;

내부 액세스 지정자가 있는 모든 멤버는 해당 멤버가 정의된 응용 프로그램 내에 정의된 모든 클래스 또는 메서드에서 액세스할 수 있습니다.

예시

using System;
namespace RectangleApplication {
   class Rectangle {
      //member variables
      internal double length;
      internal double width;
      double GetArea() {
         return length * width;
      }
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   } //end class Rectangle
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.length = 4.5;
         r.width = 3.5;
         r.Display();
         Console.ReadLine();
      }
   }
}

출력

Length: 4.5
Width: 3.5
Area: 15.75