C #Simple naming Guide
A good name can make yourProgramIt is easier to understand. When someone else sees this name, they can guess what you are doing. A large number of good names cannot be separated from good naming conventions, but a good naming convention must be implemented in two ways:
LReasonable. It is easy to accept and use. In this sense, a good naming convention should not be too long, it should let you spend10Several paragraphs have been written in minutes.CodeYou will be familiar with it.
LConsistent style. That is, they are consistent, with fewer exceptions.
It is good to comply with the naming rules above, although their specific rules may be different. Sometimes, naming conventions reflect programming ideas. For example, for interfaces. NetGenerally,IStartJavaDoes not have this requirement, becauseJavaIt is considered that interfaces and abstract classes can often be converted. For example, in the initial stage of a project, an interface may be defined, but it may be changed to an abstract class later. If the names are the same, this avoids name changes.
In addition, different naming rules also reflect the program development process. For example, the Hungarian naming method requires a type prefix.C ++,VB,PBGenerally, a large number of type prefixes appear, suchIntamount, strname. However, this requirement is generally absent because:1) Some types may be modified, suchInt16ChangeInt32. In addition, for some dynamic languages (Python,Javascript), Its type is changed and cannot be determined during compilation;2) The current development environment is more friendly. Even during development, you can easily know the type of the variable (as long as you move the mouse up ).
The naming rules in this article are mainly based on Microsoft's standards, and some practical development experiences are added. It is still being maintained and improved. You can adjust the requirements of different projects.
Naming Style Style Classification
Name |
Explanation |
Applicable Environment |
Example |
PascalCase sensitivity |
The first letter and the first letter of each subsequent word are in upper case; the other letters are in lower case. |
|
Backcolor |
CamelCase sensitivity |
Lowercase letters, whereas the first letter of each word. |
|
Backcolor |
Uppercase |
All uppercase letters |
LAbbreviation. LLess than or equal2Characters |
System. Io System. Web. UI |
Style Select
Identifier |
Naming format |
Example |
Remarks |
Parameters and local variables |
Camel |
Public IntGettypeid (StringTypename) {IntTypeid;...} |
|
Private,ProtectedInstance field |
Camel |
Private StringRedvalue; Protected BoolIsok; |
|
Interface Control |
Camel |
Private LabelLblalarm; |
|
Abbreviations |
Uppercase |
NamespaceSystem. Io {...} |
The abbreviation must be a widely accepted and well-known abbreviation. |
ExceptOut of identifier |
Pascal |
PublicSystem. Drawing.ColorBackcolor {...} |
|
Principles and habits First principle
Name meaningful and descriptive words. A glanceSee what it does. Do not use a name that may cause misunderstanding. If the name is clear, you do not need to use documents to explain the functions of the method.
1.Except for conventional ones, do not use abbreviations. UseName, address, salaryAnd so onNam, ADDR, Sal
2.Except for loops, do not use variables of a single letterI, n, xAnd so on.To useIndex, temp.
For (INT I = 0; I <count; I ++ ){...}
Other habits
LIn addition to interface controls, do not use the type prefix. For example, use the nameAmountInsteadIntamount;
LClass: Use nouns and phrases for naming. For example:PublicClass filestream;
LMethod: Start with a verb or verb phrase. For example:Createuser (), removeat ()And so on;
LInterface:IStart with a noun, noun phrase, or adjective. For example:Idisposable;
LConstant: Uppercase for all words, used between multiple words"_".Public const string page_title = "welcome ";
LNamespace:
1.Basic Format:
CompanyName/projectname.Policyname [.Feature] [.Design]
A)CompanyName/projectname: Company name, project name, or product name;
B)Policyname: Stable and recognized technical name or architectural level name;
C)[. Feature] [. Design]: Optional functions and designs;
For example:
//MicrosoftOfWebUnder developmentUIInDesign
NamespaceMicrosoft. Web. UI. Design
//Siemens quotation itemsProject Management in the business logic layer
NamespaceSiemensquotation. businessrules. projectmanagement
2.Rules
A)Nested namespaces should have dependencies. For example:System. Web. UI. Design The class in is dependent on System. Web. UI But cannot be the opposite.
B)The same name cannot be used for namespaces and classes. For example, namespaceDebugIt is better not to name another classDebug.
C)If the company name, project name, or product name is too long, you can use the abbreviation(All uppercase letters).
Control name
You need to add a prefix to the control name to distinguish between interface controls and common objects. Because interface modifications are often very frequent, separating them can help you quickly find the control, to modify the interface. For example:
The interface has10ItemsTextbox,10ItemsLabel. OneTextboxCorresponds toLabelTo complete read/write, you must have at least20Statements to obtain and assign values from the control. If a prefix exists, enterThis.txtYou can almost immediately find this control. Otherwise, the window or page object has a large number of attributes and variables (> 100You will spend a lot of time searching. AnotherTextboxCorresponds toLabel(One represents the value and the other represents the name). They usually refer to the same thing. If there is a prefix, it is easy to distinguish the two controls.
Widget |
Prefix |
Remarks |
Label |
LBL |
For example:Lblname |
Textbox |
Txt |
|
Button |
BTN |
|
Checkbox |
Chk |
|
Radiobutton |
Rdo |
|
Checkboxlist |
Chklst |
|
Radiobuttonlist |
Rdolst |
|
ListBox |
LST |
|
Dropdownlist |
DDL |
|
DataGrid |
DG |
|
Datalist |
DL |
|
Image |
IMG |
|
Table |
TBL |
|
Panel |
Pnl |
|
Linkbutton |
Lnkbtn |
|
Imagebutton |
Imgbtn |
|
Calender |
ClD |
|
Requiredfieldvalidator |
Rfv |
|
Comparevalidator |
CV |
|
Rangevalidator |
RV |
|
Regularexpressionvalidator |
Rev |
|
Validatorsummary |
VS |
|