Access Modifiers (Specifiers) ใน C# พร้อมตัวอย่างโปรแกรม

⚡ สรุปอย่างชาญฉลาด

Access modifiers in C# are keywords that set the visibility of a class, property, or method, restricting which external programs can reach them. Constructors initialize field values automatically whenever an object of the class is created.

  • 🔐 ส่วนตัว: The private access modifier limits a member to the same class, blocking access from any external program.
  • 🌐 สาธารณะ: The public access modifier exposes a property or method to any external program without restriction.
  • 🛡️ มีการป้องกัน: The protected access modifier shares members only with classes inherited from the current class.
  • 📦 ภายใน: The internal access modifier permits access within the same assembly but not from an external program.
  • 🏗️ ผู้สร้าง: A constructor shares the class name, takes no return type, and initializes fields when an object is created.
  • 🤖 ความช่วยเหลือจากเอไอ: GitHub Copilot scaffolds modifiers and constructors, while ML.NET maps class properties to model features.

Access Modifiers (Specifiers) in C#

Access Modifier (ตัวระบุ) ใน C # คืออะไร

ตัวแก้ไขการเข้าถึง or Access Specifiers in C# are the keywords used to define the visibility of a class property or method. It is used when you do not want other programs to see the properties or methods of a class. Access modifiers restrict access so that other programs cannot see the properties or methods of a class.

ตัวแก้ไขการเข้าถึงมี 6 ประเภทใน C#:

  • Private
  • สาธารณะ
  • มีการป้องกัน
  • ภายใน
  • ป้องกันภายใน
  • ได้รับการคุ้มครองส่วนตัว

เราจะเรียนรู้เกี่ยวกับตัวแก้ไขการเข้าถึงหลักใน C# พร้อมตัวอย่างโปรแกรมตามที่อธิบายไว้ด้านล่าง

ตัวแก้ไขการเข้าถึงส่วนตัวใน C #

เมื่อแนบตัวแก้ไขการเข้าถึงส่วนตัวกับคุณสมบัติหรือวิธีการ หมายความว่าสมาชิกเหล่านั้นไม่สามารถเข้าถึงได้จากโปรแกรมภายนอกใดๆ

ตัวอย่างตัวแก้ไขการเข้าถึงส่วนตัว

ลองยกตัวอย่างและดูว่าเกิดอะไรขึ้นเมื่อเราใช้ตัวแก้ไขการเข้าถึงส่วนตัว

มาแก้ไขโค้ดปัจจุบันในไฟล์ Tutorial.cs ของเรากัน ในวิธี SetTutorial เราจะเปลี่ยนคำสำคัญสาธารณะเป็นส่วนตัว

ตัวดัดแปลงการเข้าถึงส่วนตัว

ตอนนี้เรามาสลับไปใช้ไฟล์ Program.cs ของเรากันดีกว่า คุณจะสังเกตเห็นว่ามีเส้นหยักสีแดงอยู่ใต้วิธี SetTutorial

เนื่องจากเราได้ประกาศเมธอด SetTutorial เป็นแบบส่วนตัวในคลาส Tutorial แล้ว Visual Studio จึงตรวจพบสิ่งนี้ และได้แจ้งผู้ใช้โดยไฮไลต์ว่าเมธอดนี้จะใช้งานจากไฟล์ Program.cs ไม่ได้

ตัวแก้ไขการเข้าถึงส่วนตัว

ตัวดัดแปลงการเข้าถึงสาธารณะ C #

เมื่อแนบตัวแก้ไขการเข้าถึงแบบสาธารณะกับคุณสมบัติหรือวิธีการ หมายความว่าสมาชิกเหล่านั้นสามารถเข้าถึงได้จากโปรแกรมภายนอกใดๆ เราได้เห็นสิ่งนี้แล้วในตัวอย่างก่อนหน้านี้ของเรา

ตัวอย่างตัวแก้ไขการเข้าถึงสาธารณะ

ตัวดัดแปลงการเข้าถึงสาธารณะ C #

เนื่องจากเราได้กำหนดวิธีการของเราเป็นแบบสาธารณะในคลาส Tutorial จึงสามารถเข้าถึงได้จากไฟล์ Program.cs

ตัวแก้ไขการเข้าถึงที่ได้รับการป้องกันใน C #

เมื่อตัวแก้ไขการเข้าถึงที่ได้รับการป้องกันถูกแนบไปกับคุณสมบัติหรือวิธีการ หมายความว่าสมาชิกเหล่านั้นสามารถเข้าถึงได้โดยเท่านั้น คลาสที่สืบทอดมา จากปัจจุบัน ชั้น- เราจะอธิบายรายละเอียดเพิ่มเติมในคลาส Inheritance

ตัวดัดแปลงการเข้าถึงภายใน C #

เมื่อมีการแนบตัวแก้ไขการเข้าถึงภายในกับคุณสมบัติหรือวิธีการ สมาชิกเหล่านั้นสามารถเข้าถึงได้โดยโปรแกรมภายในเท่านั้น แต่ไม่สามารถเข้าถึงได้โดยโปรแกรมภายนอก

ตัวสร้าง C#

C# ตัวสร้างใช้ในการเริ่มต้นค่าของฟิลด์คลาสเมื่อวัตถุที่เกี่ยวข้องถูกสร้างขึ้น Constructor คือ method ที่มีชื่อเดียวกับ class หากมีการกำหนด Constructor ในคลาส มันจะจัดเตรียมเมธอดแรกซึ่งจะถูกเรียกเมื่อสร้างอ็อบเจ็กต์ สมมติว่าเรามีคลาสที่เรียกว่า Employee วิธีการสร้างก็จะตั้งชื่อเป็น Employee()

สิ่งสำคัญต่อไปนี้จำเป็นต้องสังเกตเกี่ยวกับวิธีการสร้าง

  1. ตัวแก้ไขการเข้าถึงเริ่มต้นของ C# สำหรับตัวสร้างจำเป็นต้องทำให้เป็นสาธารณะ
  2. ไม่ควรมีประเภทการส่งคืนสำหรับวิธีคอนสตรัคเตอร์

ตัวอย่างของตัวสร้าง C#

ตอนนี้เรามาดูกันว่าเราจะรวมผู้ใช้ Constructor เข้ากับโค้ดของเราได้อย่างไร เราจะใช้ตัวสร้างเพื่อเริ่มต้นฟิลด์ TutorialID และ TutorialName เป็นค่าเริ่มต้นบางค่าเมื่อวัตถุถูกสร้างขึ้น

ขั้นตอน 1) ขั้นตอนแรกคือการสร้าง Constructor สำหรับคลาส Tutorial ของเรา ในขั้นตอนนี้ เราจะเพิ่มโค้ดด้านล่างลงในไฟล์ Tutorial.cs

ตัวสร้าง C#

Code คำอธิบาย:-

  1. ก่อนอื่นเราเพิ่มวิธีการใหม่ซึ่งมีชื่อเดียวกันกับคลาส เนื่องจากเป็นชื่อเดียวกันกับคลาส C# จึงถือว่าสิ่งนี้เป็นเมธอดคอนสตรัคเตอร์ ดังนั้นเมื่อใดก็ตามที่เมธอดการเรียกสร้างอ็อบเจ็กต์ของคลาสนี้ เมธอดนี้จะถูกเรียกตามค่าเริ่มต้น
  2. ใน Tutorial Constructor เรากำลังตั้งค่าของ TutorialID เป็น 0 และ TutorialName เป็น "Default" ดังนั้นเมื่อใดก็ตามที่วัตถุถูกสร้างขึ้น ฟิลด์เหล่านี้จะมีค่าเริ่มต้นเหล่านี้เสมอ

ตอนนี้เรามาสลับไปที่ไฟล์ Program.cs ของเราแล้วลบบรรทัดซึ่งเรียกเมธอด SetTutorial ออก เนื่องจากเราต้องการเพียงดูว่า Constructor ทำงานอย่างไร

ตัวสร้าง C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Tutorial
 {
  public int TutorialID; 
  public string TutorialName;
  
  public Tutorial()
  {
   TutorialID=0;
   TutorialName="Default";
  }
  public void SetTutorial(int pID,string pName) 
  {
   TutorialID=pID;
   TutorialName=pName;
  }
  public String GetTutorial()
  {
   return TutorialName;
  }
  
  static void Main(string[] args) 
  {
   Tutorial pTutor=new Tutorial();
    
   Console.WriteLine(pTutor.GetTutorial());
    
   Console.ReadKey(); 
  }
 }
}

Code คำอธิบาย:-

  1. ขั้นตอนแรกคือการสร้างออบเจ็กต์สำหรับคลาส Tutorial ซึ่งทำได้โดยใช้คำสำคัญ 'ใหม่'
  2. เราใช้เมธอด GetTutorial ของคลาส Tutorial เพื่อรับค่า TutorialName จากนั้นจะแสดงค่านี้ไปยังคอนโซลผ่านเมธอด Console.WriteLine

หากป้อนโค้ดด้านบนถูกต้องแล้วและดำเนินการโปรแกรมจะแสดงผลลัพธ์ต่อไปนี้

Output:

ตัวสร้าง C#

จากผลลัพธ์ เราจะเห็นได้ว่า Constructor ถูกเรียกจริง ๆ และค่าของ TutorialName ถูกตั้งค่าเป็น "Default"

หมายเหตุ ที่นี่ค่า "ค่าเริ่มต้น" จะถูกดึงมาจากตัวสร้าง

คำถามที่พบบ่อย

If no modifier is specified, class and struct members default to private and top-level types to internal, staying hidden until you widen their visibility.

Protected exposes a member to the same class and any derived class, even across assemblies. Internal exposes it only to types in the same assembly.

Protected internal allows access from the same assembly or any derived class. Private protected is stricter, allowing only derived classes inside the same assembly.

C# supports default, parameterized, copy, static, and private constructors. They initialize an object’s fields, run class setup once, or clone another object’s values.

A static constructor takes no modifier or parameters. It runs once, before the first object is created or static members load, initializing static fields.

Yes. A class can define several constructors with different parameter lists. The compiler picks the one matching the arguments passed to new.

Yes. GitHub Copilot suggests modifiers, constructors, and field initializers from a class name or comment inside Visual Studio and VS Code. Review each suggested visibility level.

ML.NET maps the public properties of a C# data class to model features and labels. Public getters and setters let it read those values during training.

สรุปโพสต์นี้ด้วย: