在 C# 中,public、private、protected 和没有访问修饰符有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/614818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:19:11  来源:igfitidea点击:

In C#, what is the difference between public, private, protected, and having no access modifier?

c#.netasp.netaccess-modifiers

提问by MrM

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

我所有的大学四年我一直在使用public,并想知道的区别publicprivateprotected

Also what does staticdo as opposed to having nothing?

还有什么比什么都static没有呢?

采纳答案by mbillard

Access modifiers

访问修饰符

From docs.microsoft.com:

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.

public

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

private

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

protected

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

private protected(在 C# 7.2 中添加)

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

internal

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

protected internal

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

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

如果设置访问修饰符,则使用默认访问修饰符。所以总有某种形式的访问修饰符,即使它没有设置。

staticmodifier

static修饰符

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.

类上的 static 修饰符意味着该类不能被实例化,并且它的所有成员都是静态的。一个静态成员有一个版本,不管它的封闭类型有多少实例被创建。

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.

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

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(...);

回答by jpfollenius

Those access modifiers specify where your members are visible. You should probably read this up. Take the link given by IainMH as a starting point.

这些访问修饰符指定您的成员在哪里可见。你可能应该阅读这个。以 IainMH 给出的链接为起点。

Static members are one per class and not one per instance.

静态成员是每个类一个,而不是每个实例一个。

回答by gbianchi

mmm...

嗯...

Static means that you can access that function without having an instance of the class.

静态意味着您可以在没有类的实例的情况下访问该函数。

You can access directly from the class definition.

您可以直接从类定义访问。

回答by CraigTP

Hmm.

唔。

See here: Access Modifiers.

请参阅此处: 访问修饰符

In a nutshell:

简而言之:

Public gives the method or type complete visibility from other types/classes.

Public 为方法或类型提供了其他类型/类的完全可见性。

Private allows only the type containing the private method/variable access to the private method/variable (note that nested classes also have access to the containing classes private methods/variables).

Private 只允许包含私有方法/变量的类型访问私有方法/变量(注意嵌套类也可以访问包含类的私有方法/变量)。

Protected is similar to private except derived classes can also access protected methods.

Protected 与 private 类似,只是派生类也可以访问受保护的方法。

"Nothing" is VB.NET's equivalent to null. Although if you're referring to "nothing" meaning "no access modifier", then it depends, although a very rough rule of thumb (certainly in C#) is that if you don't explicitly specify an access modifier, the method/variable declaration is usually as restrictedas it can be. i.e.

“Nothing”相当于 VB.NET 的 null。尽管如果您指的是“无”意味着“没有访问修饰符”,那么这取决于,尽管一个非常粗略的经验法则(当然在 C# 中)是,如果您没有明确指定访问修饰符,方法/变量声明通常会受到尽可能的限制。IE

public class MyClass
{
    string s = "";
}

is effectively the same as:

实际上等同于:

public class MyClass
{
    private string s = "";
}

The linked MSDN article will offer a fully description when there's no access modifier explicitly specified.

当没有明确指定访问修饰符时,链接的 MSDN 文章将提供完整的描述。

回答by Tony

public- can be access by anyone anywhere.
private- can only be accessed from with in the class it is a part of.
protected- can only be accessed from with in the class or any object that inherits off of the class.

公共- 任何人都可以在任何地方访问。
private- 只能从它所属的类中访问。
受保护- 只能从类或从类继承的任何对象中访问。

Nothing is like null but in VB.
Static means you have one instance of that object, method for every instance of that class.

除了在 VB 中,没有什么比 null 更像了。
静态意味着您拥有该对象的一个​​实例,该类的每个实例的方法。

回答by Darius Kucinskas

I think it is related to good OOP design. If you are a developer of a library you want to hide the inner workings of your library. That way, you can modify your library inner workings later on. So you put your members and helper methods as private, and only interface methods are public. Methods that should be overwritten should be protected.

我认为这与良好的 OOP 设计有关。如果您是库的开发人员,您想隐藏库的内部工作原理。这样,您可以稍后修改库的内部工作。所以你把你的成员和辅助方法设为私有,只有接口方法是公有的。应该被覆盖的方法应该受到保护。

回答by Patrick Peters

Careful watch your accessibility of your classes. Public and protected classes and methods are by default accessible for everyone.

仔细观察您的课程的可访问性。默认情况下,每个人都可以访问公共和受保护的类和方法。

Also Microsoft isn't very explict in showing access modifiers (public, protected, etc.. keywords) when new classes in Visual Studio are created. So, take good care and think about your accessibility of your class because it's the door to your implementation internals.

此外,在 Visual Studio 中创建新类时,Microsoft 在显示访问修饰符(public、protected 等关键字)方面也不是很明确。因此,请小心并考虑您的类的可访问性,因为它是您实现内部的大门。

回答by JosephStyons

Public- If you can see the class, then you can see the method

公共- 如果您可以看到类,那么您可以看到方法

Private- If you are part ofthe class, then you can see the method, otherwise not.

私人- 如果您是班级的一部分,那么您可以看到该方法,否则看不到。

Protected- Same as Private, plus all descendantscan also see the method.

Protected- 与 Private 相同,而且所有后代也可以看到该方法。

Static (class)- Remember the distinction between "Class" and "Object" ? Forget all that. They are the same with "static"... the class is the one-and-only instance of itself.

静态(类)- 还记得“类”和“对象”之间的区别吗?忘记这一切。它们与“静态”相同……类是其自身的唯一实例。

Static (method)- Whenever you use this method, it will have a frame of reference independent of the actual instance of the class it is part of.

静态(方法)- 每当您使用此方法时,它都会有一个独立于它所属的类的实际实例的参考框架。

回答by leppie

Regarding the question of Nothing

关于一事无成的问题

  • Namespace types are internal by default
  • Any type member, including nested types are private by default
  • 命名空间类型默认是内部的
  • 任何类型成员,包括嵌套类型默认都是私有的

回答by Grant Hood

A status of Private indicates that variables can only be accessed by objects of the same class. Protected status extends that access to include descendants of the class as well.

Private 状态表示变量只能由同一类的对象访问。受保护状态将访问权限扩展到包括该类的后代。

"from the above table we can see the deference between private and protected... am think both are same ....so what the need for that two separate command"

“从上表中我们可以看到私有和受保护之间的区别......我认为两者是相同的......所以需要这两个单独的命令”

Check MSDNlink for more information

查看MSDN链接以获取更多信息