Summary of modifiers in the C language

  • 2021-08-21 21:08:43
  • OfStack

A modifier is a symbol used to qualify types and declarations of type members. The following are mainly introduced from four kinds of access modifiers in C #, modifiers acting on classes and structures, modifiers used in methods or variables and modifiers with special functions.

1. Access modifiers

Specifies the accessibility of declared types and type members.

(1) public: Is an access modifier for types and type members. Public access is the highest level of access allowed. There are no restrictions on access to public members.

(2) private: Is a member access modifier. Private access is the lowest level of access allowed. Private members are accessible only in the class and structure in which they are declared.

(3) internal: Is an access modifier for types and type members. Internal types or members are accessible only in files with the same 1 assembly.

(4) protected: Is a member access modifier. A protected member is accessible in its class and by derived classes.

2. Modifiers that can be applied to classes and constructs

(1) abstract: Indicates that a class can only be the base class of other classes.

It can be used with classes, methods, properties, indexers and events. Use the abstract modifier in class declarations to indicate that a class can only be the base class of other classes. Members marked as abstract or contained in abstract classes must be implemented through classes derived from abstract classes.

(2) sealed: Specifies that the class cannot be inherited.

Can be applied to classes, instance methods, and properties. Sealed classes cannot be inherited. Sealed methods override methods in the base class, but they themselves cannot be overridden one step further in any derived class. When applied to a method or property, the sealed modifier must always be used from override1.

Using the sealed modifier in class declarations prevents inheritance of this class

(3) static: Declares members that belong to the type itself rather than to a specific object.

Declares static members that belong to the type itself rather than to a specific object. Can be used for classes, fields, methods, properties, operators, events, and constructors, but not for indexers, destructors, or types other than classes.

(4) partial: Defines partial classes and structures throughout the same 1 assembly.

Partial type definitions allow the definition of a class, structure, or interface to be split into multiple files.

3. Modifiers that can only be used on methods or variables

(1) const: Specifies that the value of a field or local variable cannot be modified.

Declarations used to modify fields or local variables. It specifies that the value of a field or local variable is a constant and cannot be modified.

(2) new: Hide inherited members from base class members.

When used as a modifier, the new keyword can explicitly hide members inherited from the base class. Hiding an inherited member means that a derived version of the member will replace the base class version. Hiding members without the new modifier is allowed, but a warning is generated. Using new to explicitly hide a member cancels this warning and records the fact that it is replaced by a derived version.

To hide an inherited member, declare the member in a derived class with the same name and modify the member with the new modifier.

Using both new and override for the same 1 member is wrong because the two modifiers are mutually exclusive in meaning. Using new creates a new member with the same name and makes the original member hidden, while override extends the implementation of inherited members.

(3) virtual: Declares in a derived class a method or accessor whose implementation can be changed by an overridden member.

Used to decorate a method, property, indexer, or event declaration and to allow these objects to be overridden in a derived class.

When a virtual method is called, the runtime type of the object is checked for overriding members. This overridden member in most derived classes will be called, and if no derived class overrides this member, it may be the original member.

By default, methods are non-virtual. You cannot override a non-virtual method.

The virtual modifier cannot be used with the static, abstract, and override modifiers 1.

Except for the difference in declaration and call syntax, virtual attributes behave like abstract methods 1.

(4) override: Provides a new implementation of virtual members inherited from the base class.

To extend or modify an abstract or virtual implementation of an inherited method, property, indexer, or event, you must use the override modifier.

(5) readonly: Declare a field that can only be assigned as part of the declaration or in a constructor of the same class.

Is a modifier that can be used on a field. When a field declaration includes an readonly modifier, the field assignment introduced by the declaration can only appear as part of the declaration or in a constructor of the same class. In this example, the value of the field year cannot be changed in the ChangeYear method, even if it is assigned a value in the class constructor.

4. Modifiers with special functions

(1) event: Declare an event.

Classes and structures use events to notify objects of events that may affect their state.

(2) extern: Indicates that the method is implemented externally.

Used to declare methods that are implemented externally. The common use of the extern modifier is in conjunction with the DllImport property 1 when calling in unmanaged code using the Interop service; In this case, the method must also be declared as static.

The extern keyword also defines an external assembly alias so that different versions of the same 1 component can be referenced from a single assembly.

It is an error to use the abstract and extern modifiers 1 together to modify the same 1 member. Using the extern modifier means that the method is implemented outside the C # code, while using the abstract modifier means that no method implementation is provided in the class.

(3) unsafe: Declares an insecure context. This context is required for any operation involving pointers.

(4) volatile: Indicates that the field can be modified in the program by the operating system, hardware, or concurrent execution threads.

The volatile keyword indicates that a field may be modified by multiple concurrent execution threads. Fields declared as volatile are not subject to compiler optimization (assuming access by a single thread). This ensures that the field is rendered with the latest value at all times.

The volatile modifier is typically used for fields accessed by multiple threads without the use of the lock statement (C # reference) statement to serialize access.

The volatile keyword can be applied to the following types:

(1) Reference type.

(2) Pointer type (in an unsafe context).

(3) Integer types, such as sbyte, byte, short, ushort, int, uint, char, float and bool.

(4) Enumeration types with integer base types.

(5) Generic type parameters known as reference types.

(6) IntPtr and UIntPtr.

The type involved must be a field of a class or structure. You cannot declare a local variable as volatile.

The above is all about C # modifiers, hoping to help everyone learn.


Related articles: