C#访问修饰符或说明符是用于指定C#应用程序中变量和函数的可访问性或范围的关键字。
C#提供了五种类型的访问说明符。
无涯教程可以选择其中任何一个来保护数据。下表描述了每个组件的可访问性。
Access Specifier | Description |
---|---|
Public | 它指定不受限制的访问。 |
Protected | 它指定访问权限仅限于包含类或派生类。 |
Internal | 它指定访问权限限于当前程序集。 |
protected internal | 它指定访问权限仅限于当前汇编或从包含类派生的类型。 |
Private | 它指定访问范围仅限于包含类型。 |
现在,创建示例来检查每个访问说明符的可访问性。
它使数据可以公开访问。
using System; namespace AccessSpecifiers { class PublicTest { public string name = "Shantosh Kumar"; public void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { PublicTest publicTest = new PublicTest(); // Accessing public variable Console.WriteLine("Hello " + publicTest.name); // Accessing public function publicTest.Msg("Peter Decosta"); } } }
输出:
Hello Shantosh Kumar Hello Peter Decosta
它可以在类内访问,并且范围有限。在继承的情况下,也可以在子类或子类中访问它。
using System; namespace AccessSpecifiers { class ProtectedTest { protected string name = "Shashikant"; protected void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { ProtectedTest protectedTest = new ProtectedTest(); // Accessing protected variable Console.WriteLine("Hello "+ protectedTest.name); // Accessing protected function protectedTest.Msg("Swami Ayyer"); } } }
输出:
Compile time error 'ProtectedTest.name' is inaccessible due to its protection level.
示例2
在这里,无涯教程通过继承访问子类中的受保护成员。
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-access-modifiers.html
来源:LearnFk无涯教程网
using System; namespace AccessSpecifiers { class ProtectedTest { protected string name = "Shashikant"; protected void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program : ProtectedTest { static void Main(string[] args) { Program program = new Program(); // Accessing protected variable Console.WriteLine("Hello " + program.name); // Accessing protected function program.Msg("Swami Ayyer"); } } }
输出:
Hello Shashikant Hello Swami Ayyer
INTERNAL关键字用于指定变量和函数的内部访问说明符。此说明符只能在同一程序集中的文件中访问。
using System; namespace AccessSpecifiers { class InternalTest { internal string name = "Shantosh Kumar"; internal void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { InternalTest internalTest = new InternalTest(); // Accessing internal variable Console.WriteLine("Hello " + internalTest.name); // Accessing internal function internalTest.Msg("Peter Decosta"); } } }
输出:
Hello Shantosh Kumar Hello Peter Decosta
声明了受保护的内部的变量或函数可以在声明它的程序集中访问。也可以在另一个程序集中的派生类中访问它。
using System; namespace AccessSpecifiers { class InternalTest { protected internal string name = "Shantosh Kumar"; protected internal void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { InternalTest internalTest = new InternalTest(); // Accessing protected internal variable Console.WriteLine("Hello " + internalTest.name); // Accessing protected internal function internalTest.Msg("Peter Decosta"); } } }
输出:
Hello Shantosh Kumar Hello Peter Decosta
私有访问说明符用于指定变量或函数的私有可访问性。它是限制性最强的,并且只能在声明它的类体中访问。
using System; namespace AccessSpecifiers { class PrivateTest { private string name = "Shantosh Kumar"; private void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { PrivateTest privateTest = new PrivateTest(); // Accessing private variable Console.WriteLine("Hello " + privateTest.name); // Accessing private function privateTest.Msg("Peter Decosta"); } } }
输出:
Compile time error 'PrivateTest.name' is inaccessible due to its protection level.
using System; namespace AccessSpecifiers { class Program { private string name = "Shantosh Kumar"; private void Msg(string msg) { Console.WriteLine("Hello " + msg); } static void Main(string[] args) { Program program = new Program(); // Accessing private variable Console.WriteLine("Hello " + program.name); // Accessing private function program.Msg("Peter Decosta"); } } }
输出:
Hello Shantosh Kumar Hello Peter Decosta
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)