📅  最后修改于: 2020-10-31 03:13:35             🧑  作者: Mango
C#访问修饰符或说明符是用于指定C#应用程序中变量和函数的可访问性或范围的关键字。
C#提供了五种类型的访问说明符。
我们可以选择任何一种来保护我们的数据。公共不受限制,私人受最大限制。下表介绍了每种服务器的可访问性。
Access Specifier | Description |
---|---|
Public | It specifies that access is not restricted. |
Protected | It specifies that access is limited to the containing class or in derived class. |
Internal | It specifies that access is limited to the current assembly. |
protected internal | It specifies that access is limited to the current assembly or types derived from the containing class. |
Private | It specifies that access is limited to the containing type. |
现在,让我们创建示例来检查每个访问说明符的可访问性。
它使数据可以公开访问。它不会将数据限制为已声明的块。
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
在这里,我们通过继承访问子类中受保护的成员。
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
声明为protected internal的变量或函数可以在声明了该变量或函数的程序集中进行访问。也可以在另一个程序集中的派生类中访问它。
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