在 C# 中,public、private、protected 和没有访问修饰符有什么区别? [英] In C#, what is the difference between public, private, protected, and having no access modifier?

查看:21
本文介绍了在 C# 中,public、private、protected 和没有访问修饰符有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大学时代一直在使用public,想知道publicprivate的区别受保护?

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

还有什么是 static 的作用而不是什么都没有?

Also what does static do as opposed to having nothing?

推荐答案

访问修饰符

来自 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.

私有

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

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

受保护

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

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

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

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.

内部

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

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

受保护的内部

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

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.

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

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.

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

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

这篇关于在 C# 中,public、private、protected 和没有访问修饰符有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆