ตัวระบุการเข้าถึงภายใน
ตัวระบุการเข้าถึงภายในช่วยให้คลาสแสดงตัวแปรสมาชิกและฟังก์ชันของสมาชิกไปยังฟังก์ชันและอ็อบเจ็กต์อื่นๆ ในแอสเซมบลีปัจจุบันได้
สมาชิกที่มีตัวระบุการเข้าถึงภายในสามารถเข้าถึงได้จากคลาสหรือเมธอดใดๆ ที่กำหนดไว้ภายในแอปพลิเคชันที่กำหนดสมาชิกไว้
ต่อไปนี้เป็นตัวอย่าง −
ตัวอย่าง
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();
}
}
} ตัวระบุการเข้าถึงแบบส่วนตัว
ตัวระบุการเข้าถึงแบบส่วนตัวอนุญาตให้คลาสซ่อนตัวแปรสมาชิกและฟังก์ชันสมาชิกจากฟังก์ชันและอ็อบเจ็กต์อื่นๆ เฉพาะฟังก์ชันของคลาสเดียวกันเท่านั้นที่เข้าถึงสมาชิกส่วนตัวได้
ต่อไปนี้เป็นตัวอย่าง −
ตัวอย่าง
using System;
namespace RectangleApplication {
class Rectangle {
private double length;
private double width;
public void Acceptdetails() {
length = 10;
width = 15;
}
public double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}