Technical summary: C # grammar optimization


*1. Entity classes use attributes instead of fields

*1.1 error example:

Technical summary: C # grammar optimization

*1.2 correct example:

Technical summary: C # grammar optimization

In fact, the latter is very simple and comfortable. When there are many private fields, the amount of code of this class is very ugly.

*2. Correct use of extension method

Technical summary: C # grammar optimization

Wrong usage: class name. The static method
Correct way: This to-do object calls this static method directly

Difference: Although the two methods can be used normally, the original intention of the design is to extend a class method rather than define a tool class. In addition, once the class changes, the correct way of naming the space will not report an error, but the wrong way needs to be modified everywhere.

To correct the second method, it is necessary to introduce the namespace of the expansion method. It is recommended that all expansion methods be placed in the same folder

*3. Entrustment

*3.1 what is commission

In terms of data structure, delegation is a user-defined type just like class.

A delegate is an abstraction of a method, which stores the addresses of a series of methods with the same signature and return type. When a delegate is called, all the methods contained in the delegate are executed.

*3.2 definition of entrustment

(delegate characteristics: 1. Polymorphism = > delegate class or method 2. Very much like the policy pattern in Java)
A delegate is a type, just as a class is a type. Like classes, delegate types must be declared before they are used to create variables and type objects.

The declaration prototype of the delegate is
Delegate < function return type > < delegate name > (< function parameters >)

Technical summary: C # grammar optimization

This article comes from: Song Wenchao super, exclusive platforms are CSDN, segment fault, Jianshu, open source China, please indicate the source of reprint.

Recommended Today

Install and configure Tomcat

Download, install and configure Tomcatone . download Download address: Choose 32-bit or 64 bit according to your computer You can download the installation free version (zip), or the installation version (. Exe) two . install If you download the installation free version, you can unzip it directly after downloading Start: find the startup.bat Click […]