私有类在C#概念概念

2023-09-03 06:37:02 作者:单身成瘾

可以在C#中存在的私有类,除了在内部类?

Can private classes exist in C#, other than in Inner classes?

推荐答案

根本就没有。没有,除非它在嵌套类

Simply NO. Nothing unless its in a nested Class

类和结构不嵌套在其他类或结构内的可以是公开或内部。声明为public A型是任何其他类型的访问。声明为内部A型仅由同一个程序集内的类型进行访问。类和结构被声明为内部默认,除非关键字市民被添加到类定义。

Classes and structs that are not nested within other classes or structs can be either public or internal. A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by default unless the keyword public is added to the class definition.

类或结构定义可以添加内部关键字,使他们的访问级别明确。访问修饰符不影响类或结构本身 - 它总是能够访问自己和自己所有成员

Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself — it always has access to itself and all of its own members.

结构成员,包括嵌套类和结构,可以声明为public,internal或private。类成员,包括嵌套类和结构,可以是公共的,受保护的内部,保护的,内部的,或私有。对于类成员和结构成员,包括嵌套类和结构的访问级别,是私有的默认值。私人嵌套类型不是从包含类型的外部访问。

Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.

派生类不能比它们的基础类型更大的便利。换句话说,你不能有一个公共的B级,从一个内部类A派生如果被允许,那就做一个公开,因为所有受保护或内部的成员是从派生类访问的效果。 您可以启用特定的其他组件使用来访问你的内部类型的InternalsVisibleToAttribute.

Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class. You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute.