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