且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中,公共,私有,受保护和没有访问修饰符之间有什么区别?

更新时间:2023-02-15 17:46:16

访问修饰符



来自 docs.microsoft.com


> public



该类型或成员可以由同一程序集或引用该程序集的另一个程序集中的任何其他代码访问。



private



该类型或成员只能由相同类或结构中的代码访问。



受保护的



类型或成员只能由相同类或结构或派生类中的代码访问。 / p>

受私有保护 (C#7.2中已添加)



该类型或成员只能由同一类或结构中的代码访问,或者只能由同一程序集中的派生类访问,而不能从另一个程序集中访问。



内部



类型或成员可以由同一程序集中的任何代码访问,但不能从另一个程序集中访问。



受保护的内部



该类型或成员可以由同一程序集中的任何代码访问,也可以由另一个程序集中的任何派生类访问。


设置无访问权限修饰符时,将使用默认访问修饰符。因此,即使没有设置访问修饰符,总会有某种形式。



静态修饰符



类上的static修饰符意味着该类无法实例化,并且其所有成员都是静态的。静态成员具有一个版本,无论创建了多少个其封闭类型的实例。



静态类与非静态类基本相同,但是存在一个区别是:静态类不能在外部实例化。换句话说,您不能使用new关键字来创建类类型的变量。因为没有实例变量,所以您可以使用类名本身来访问静态类的成员。



但是,存在诸如静态构造函数。任何类都可以具有其中之一,包括静态类。它们不能直接称为&不能有参数(类本身上没有任何类型参数)。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化类。看起来像这样:

 静态类Foo()
{
静态Foo()
{
Bar = fubar;
}

公共静态字符串Bar {get;组; }
}

静态类通常用作服务,您可以像这样使用它们:

  MyStaticClass.ServiceMethod(...); 


All my college years I have been using public, and would like to know the difference between public, private, and protected?

Also what does static do as opposed to having nothing?

Access modifiers

From docs.microsoft.com:

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private

The type or member can only be accessed by code in the same class or struct.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

private protected (added in C# 7.2)

The type or member can only be accessed by code in the same class or struct, or in a derived class from the same assembly, but not from another assembly.

internal

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

When no access modifier is set, a default access modifier is used. So there is always some form of access modifier even if it's not set.

static modifier

The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:

static class Foo()
{
    static Foo()
    {
        Bar = "fubar";
    }

    public static string Bar { get; set; }
}

Static classes are often used as services, you can use them like so:

MyStaticClass.ServiceMethod(...);