Analysis of access modifier usage for C class

  • 2020-11-20 06:12:50
  • OfStack

This article gives a detailed analysis of the use of access modifiers for C# classes and shares them with you for your reference. Specific usage analysis is as follows:

By default, the class is declared internal, meaning that only code in the current project can access it. You can specify this explicitly with the internal access modifier keyword, but this is not required, and the class defaults to a class of this type when defined. But the C# method defaults to an access level of private.

The access level of a modifier for a method or property is shown below:

访问修饰符

 

public

访问不受限制,可以类内和任何类外的代码中访问

protected

可访问域限定于类内或从该类派生的类内

internal

可访问域限定于类所在的程序集内

protected internal

protected或者internal,即可访问域限定于类所在的程序或那些由它所属的类派生的类内

private

可访问域限定于它所属的类内

A combination of access modifiers that can be used in a class definition

The none or internal class can only be accessed in the current project
The public class can be accessed anywhere
The abstract or internal abstract class can only be accessed in the current project, cannot be instantiated, and can only be inherited
The public abstract class can be accessed anywhere, cannot be instantiated, and can only be inherited
The sealed or internal sealed class can only be accessed in the current project, cannot be derived, and can only be instantiated
The public sealed class can be accessed anywhere, cannot be derived, and can only be instantiated

The default access modifiers for methods, classes, and so on in C# are described below

Interface (interface)

The interface member access modifier defaults to public and cannot be displayed using the access modifier.

Class (class)

The constructor defaults to the public access modifier.

The destructor cannot display using the access modifier and defaults to private access modifier.

The default access modifier for class members is private;

Enumeration (enum)

Enumeration type members default to public access modifiers and cannot be displayed using modifiers.

Structure (struct).

Structure members default to the private modifier.

A structure member cannot be declared as an protected member because the structure does not support inheritance.

Nested types

The default access modifier for nested types is private. And class, the members of the structure access type 1 by default

Hopefully this article has helped you with your C# programming.


Related articles: