SoFunction
Updated on 2025-03-01

C# event standard naming rules and descriptions (including delegated naming used as event types)

C# event standard naming rules

Some open source code event naming is very confusing, and this article is used as a reference for my future work.

The name of the event

An event always refers to an operation that may or may have occurred. Therefore, like the method, events are named with predicates, and predicate tense is used to indicate the time when the event is triggered.

✓ Make sure to usePredicate or predicate phrase to name events.

Examples: Clicked, Painting, DroppedDown, etc.

✓ Must passUse present tense and past tense to allow event names to contain the concept of time sequence.

For example, an event that is raised before a window is closed is called Closed, and an event that is raised after a window is closed is called Closed.

X Do not use the "Before" or "After" prefix and suffix to indicate before or after the event. The present tense and past tense should be used as described above.

✓ Please useThe "EventHandler" suffix names the event handler (used as a delegate for event types), as shown in the following example:

public delegate void ClickedEventHandler(object sender, ClickedEventArgs e);

✓ Make sureUse two parameters named sender and e in the event handler.

The sender parameter represents the object that raised the event. The type of the sender parameter is usually object, and a more specific type may be used.

✓ Make sureUse the "EventArgs" suffix to name the event parameter class.

Personal advice: For events with unknown name meaning, you can add Event suffixes and try to use suffixes as little as possible.

Official reference:/zh-cn/dotnet/standard/design-guidelines/names-of-type-members#names-of-events

The name of the event trigger method

An event requires an event triggering method to detect whether the event is bound, and if so, it will trigger the event. Direct contact event will raise an empty exception when the event has no bound method.

✓ Be sure to use "On+Event Name" to name the event trigger method.

C# naming specifications, variables, and class

Naming Specifications in C#

1. Naming method

Camel nomenclature: When naming a variable, the first word is all lowercase, and the first letter of the word is then capitalized. Declarations applicable to variables

For example: int imgIndex=0;  string txtPersonName="xxx";

Pascal nomenclature: the capitalization of the initial letter of all words. Applicable to all project names, object names, method names, attribute names

For example: Class BigCar, SmallCar;

2. Naming rules

"1." Create a project to use Chinese during the learning process, but Chinese and pinyin are strictly prohibited during work.

"2." Use Pascal nomenclature to naming projects (the first letter of all English words is capitalized)

"3." Declaring that variables, etc. do not allow the use of special symbols (except _) or keywords (except _)

What are variables

Variables refer to the temporary storage of some data when the program is executed, and it needs to be used anytime and anywhere as the program is executed. At this time, a space needs to be opened in the program's running memory to temporarily store the data. This is the function of variables.

1. How to declare variables

  • [Access Modifier] Data Type Variable Name = Initial Value;
  • Data type (value type and reference type): specifies the types of data allowed to be stored in this variable space
  • Variable name: It can be understood as the pointer address of a variable. You can use the variable name to find the data stored in the variable.
  • Initial value: The data stored for the first time when this variable is defined

2. Classification of variables

1. Global variables

  • A global variable refers to the declaration place of a variable directly in Class.
  • Global variable declaration cycle: Starting from the computer, the global variable is created, and the global variable is also cleaned when the Class object is released by GC.
  • Scope of variables: can be used anywhere inside this Class

2. Local variables

  • Local variables refer to the declaration of a variable in class members (methods, events, syntax structures)
  • Declaration cycle of local variables: when the computer reads the declaration code of the variable, the variable is created, and when the computer reads the scope of the variable beyond the variable, the variable is released.
  • Scope of a variable: inside the braces where it is declared
  • Minimize unnecessary global variables as much as possible. Problems that can be solved with local variables must not be used. Only when this variable is possible anywhere in an object can it be declared as a global variable.

Class

1. What is a class

  • Class refers to the general term for a class of objects, class is also an object, and class is an abstraction of objects
  • Create an object in programming, and the code written is called a class

2. How to identify the class

  • The keyword color is light green
  • Or observe that its type is Class

3. How to use the class

  • When most classes are used, they need to instantiate them and create a new member object, which has all the functions of this class.
  • The final result of instantiation is to produce a new object member of this type

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.