C# 访问修饰符或说明符是用于指定 C# 应用程序中变量和函数的可访问性或范围的关键字。
C# 提供了五种类型的访问说明符。
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");
}
}
}输出:
你好Shantosh Kumar 你好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");
}
}
}输出:
编译时错误“ProtectedTest.name”因其保护级别而无法访问。
示例 2
在这里,我们通过继承访问子类中的受保护成员。
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");
}
}
}输出:
你好Shashikant 你好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");
}
}
}输出:
你好Shantosh Kumar 你好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");
}
}
}输出:
你好Shantosh Kumar 你好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");
}
}
}输出:
编译时错误'PrivateTest.name' 由于其保护级别而无法访问。
using System;
namespace AccessSpecifiers
{
class Program
{
private string name = "Shantosh Kumar";
private void Msg(string msg)
{
Console.WriteLine("你好" + msg);
}
static void Main(string[] args)
{
Program program = new Program();
// Accessing private variable
Console.WriteLine("你好" + program.name);
// Accessing private function
program.Msg("Peter Decosta");
}
}
}输出:
你好Shantosh Kumar 你好Peter Decosta