C# study notes modifier

Access control character

In C # have access specifier 5, wherein there are four basic: public, , protected, private, internaland a compound modifiers protected internal(also can be written internal protected).


The accessibility of the members of a class has two meanings: one is based on logic, for example, protected means that its subclasses can also be accessed; the other is based on physical, that is, whether the accessibility is in the same assembly (assembly ) Is related, such as internal indicates that it is accessible in the same assembly class.

  • Assembly, also known as "program assembly", refers to putting multiple classes (compiled MSIL instructions) into the same file (usually a .dll file or an .exe file) when compiling.

The relationship between modifiers of class members and accessibility is shown in the following table:
Insert picture description here
The access control symbols that can be used for various elements are shown in the following table:
Insert picture description here

static modifier

Simply put, static is "non-instance".
You can modify fields, methods, etc.

static construction method

The construction method modified with static, that is, the static construction method, is also called "static constructor". The static construction method cannot have parameters or other modifiers. A class can only have one static constructor at most.
The difference between the static construction method of the class and the instance construction method of the class:

  • The instance constructor initializes each newly created object, and the static constructor initializes the class itself.
  • The instance construction method is automatically executed by the system when the new operator is used to generate a new object; the static construction method generally cannot be called by the program, and it is executed by the system when the class to which it belongs is loaded into the memory. The system can always ensure that the static construction method is executed before all static members, and it can also be guaranteed to execute before any instance is created.
  • Like the static method, it cannot access instance fields and instance methods.

static class and using static class

  • In C# 2.0 and above, the class can also be staticmodified, indicating that all members of the class are static. For example, System.Consoleclass, System.Mathclass.
  • C# 6.0 and above, you can import the static class, so you can omit the class name and write the method name directly. For example: write in the front using static System.Console; WriteLine() can be written directly in the back, which is equivalent to Console.WriteLine().

const and readonly

1. const modified class members

If a member of a class is modified by const, it is called a "constant". The method of declaring constants is as follows:

	修饰符 const 类型 常量名 = 常量表达式;

Constants and fields are members, but constants have some important characteristics:

  • Must be assigned at the time of definition, and cannot be assigned with variables.
  • The constant cannot be modified.
  • The types of constants can be simple types, enumerated types, and various reference types. The reference type here stringcan only be assigned if it is not null. The type of a constant cannot be a simple struct type. If you want to achieve structa constant amount of type, you can use it readonly.
  • constConstants imply staticmodifiers and can only be accessed through the class name.

constYou can modify not only class members, but also local variables.

readonly field

Compared constwith, it readonlyhas the following characteristics:

  • The readonly field can be of various types (while const can only be simple types and strings).
  • The readonly field can be assigned with variables or expressions.
  • readonly cannot modify local variables.
  • The readonly field does not imply the static nature.
  • The readonly field is not required to be initialized when it is defined. You can also assign values ​​in the construction method when assigning, including out and ref parameters for processing. But it can only be assigned once at most.

It can be seen that the readonlyrequired read-only is considered "read-only at runtime", and the constrequired read-only is "constant at compile time".
In fact, when readonlyused in programs, it is often used statictogether with them to achieve functions similar to "constants". For example, use a readonly staticfield to represent the color of "constant".

public class Color
{
    
    
	public static readonly Color Black = new Color(0, 0, 0);
	public static readonly Color White = new Color(255, 255, 255);
	public static readonly Color Red = new Color(255, 0, 0);
	public static readonly Color Green = new Color(0, 255, 0);
	public static readonly Color Blue = new Color(0, 0, 255);
	public Color(byte r, byte g, byte b){
    
    
		red = r;
		green = g;
		blue = b;
	}
}

sealed及abstract

sealed class

sealed (sealed) can be used to modify the class. If a class is sealedmodified and defined, it means that this class cannot be inherited.

abstract class

Abstract (abstract) can modify the class and some members of the class (methods, properties, indexers).
An abstract class is a conceptual class without concrete objects. Abstract classes cannot be instantiated. In order to create objects, such classes must be inherited, and then create and subclass object instances. The significance of
defining an abstract class is that an abstract class is a collection of the public properties of all its subclasses, so a major advantage of using an abstract class is that you can make full use of these public properties to improve the efficiency of development and maintenance. A System.Arrayclass in C# is an abstract class, which represents the common parent class of all arrays.
The format for defining an abstract class is as follows:

abstract class 类名
{
    
    
	...
}

Note: Although abstract classes cannot be instantiated, abstract classes can have constructors, and these constructors can be called by subclass constructors.

abstract methods, properties, indexers

The method modified by abstract is called abstract method. The function of abstract method is that all its subclasses define a unified interface. The abstract method only needs to be declared but not implemented, and the format is as follows:

abstract 类型 方法名(参数列表);

The definition format of abstract attributes is as follows:

abstract 类型 属性名
{
    
    
	get; set;
}

The definition format of the abstract indexer is as follows:

abstract 类型 this [参数列表]
{
    
    
	get; set;
}

abstractPay attention to the following points when using :

  • Abstract cannot modify the same method in parallel with static.
  • Abstract cannot modify the same method in parallel with private, nor can access control characters be omitted.
  • The abstract method must be located in the abstract class.
  • When a subclass implements an abstract method, it must be modified with override. Otherwise, it is not considered to have implemented the abstract method, but is considered to be a new (new) method.

new、virtual、override

1. new

There are newhidden members written in the front , etc...

2. virtual及override

virtualYou can modify methods, properties, and indexers. The virtualmodified members are called virtual members. A virtual member in the subclass can be overridden (override).


Use virtualand overridePrecautions:

  • Virtual and override cannot modify the same method in parallel with static.
  • Virtual and override cannot modify the same method in parallel with private, nor can they default access control characters.
  • The abstract member is implicitly virtual, but abstract cannot modify the same member in parallel with virtual.
  • When a subclass implements an abstract or virtual method, it must be modified with override, otherwise it is not considered to have implemented the abstract method, but a new (new) method.
  • An override member can be further covered in its subclasses.
  • An override member must have a corresponding virtual, abstract or override member in its direct parent class or indirect parent class.

3. sealed override

In order to prevent further coverage of the virtual method of the parent class in the subclass, you can use the sealedmethod (closed method). sealedMust overridebe used together with at this time .
E.g:

class A
{
    
    
	public virtual void M(){
    
    ...}
}
class B : A
{
    
    
	public sealed override void M(){
    
    ...}
}

Guess you like

Origin blog.csdn.net/qq_45349225/article/details/113903531