C # Advanced (1)
Named rules in .NET (with: Hungarian nomenclature)
The unified naming rules that use comments in your code and the unified naming rules you are going to say are a good habit, they will make your code more common. I (author) has worked in Microsoft for nearly five years. I don't know if you have a lot of problems with me, I have encountered a lot of problems in integration and bug ---- Because developers do not comply with unified naming rules and write comments statements.
In previous versions of Visual Studio, Microsoft recommends using Hungarian nomenclature to write code and encourages the development of this unified format code to use the same rule. E.g:
variable
name
Cstring
Szstring
charr
CMYCHAR
Char *
PMYCHAR
Long
Lmyvariable
LPCSTR
LPSTR
Int
NMYNumber
In recent release .NET and its programming language, Microsoft has replaced his rule. If you have used Delphi programming, you will see new rules similar to Delphi (Pascal).
To avoid code conflicts (this will make your code more common), you will use a naming rule, which is a good programming habit. Ok, here introduce some common rules.
◆ Name variables, methods, and attributes
The first letter of the name of the variable, method, and attribute should be capitalized, and the name should be able to express their use (or meaning).
variable
.NET named
Hungary naming
description
Cstring
EMPLOYEENAME
Szname
Name of an employee.
Int
AttenAceCounter
Ncounter
A counter of type long.
Long
Numberofbytes
lbytes
A longtes.
Sometimes we are accustomed to using underscore "_", for example: add_data (int A, int b). In accordance with new naming rules, this is not a good program habit, although it is not wrong. You should change the definition add_data to adddata. This is not the standard of Microsoft, you don't have to follow these rules. However, you will understand the rationality of these rules below.
I personally like the Hungarian nomenclature. Of course, variables also use the same law. If you remember the Hungarian nomenclature, a Boolean variable is defined in "B", such as:
Bool bflag = true;
The new rule does not recommend using Flag and "B":
Bool isfilefound = true;
You can browse MSDN, there are more instructions on new rules.
◆ Names Components and Collections (Assemblies)
To avoid code conflicts, naming your library (called Assembly in .NET) is a good programming habit according to the nomenclature. Assume that you are Mindcracker, you are developing a library for expanding the C # Database class, named its name to mindcracker.Databasesseassembly.adoset is much better than Myassembly.Database.adoset.
Suppose your library has a method, which is read from the table and returns a data set. If you are named DataSet Return_Data (), change to DataSet Returndata ().
You should also follow a unified word order in a naming. For example, you have both tables, which are Employee and Manager, and you also define a method of inserting a record in the table, then the method name AppendemPloyee and AppendManager are better than AppendemPloyee and ManageraPpend. ◆ cycle
Cycling or other block structures should be aligned with left and right braces, like this:
For (i = 0; i <100; i ) {...}
or
For (i = 0; i <100; i ) {...}
I prefer two methods because it is easy to know a piece of structure and nesting structure.
◆ Hungarian nomenclature (EFOXXX)
The Hungarian nomenclature is a Hungarian programmer, and he has worked in Microsoft for many years. This naming is passed through Microsoft's various products and documents. Most experienced programmers, no matter which door language they use, it is more or less
The basic principles of this nomenclature are:
Variable Name = Property Type Object Description
That is, a variable name consists of three parties, so that the programmer is easily understood by the type, use, and easy to memorize.
Lower is some rules examples of recommendations, you can choose to use it, you can do some modifications according to personal preferences.
(1) Attribute section:
Global variable: g_
Constant: c_
Class member variable: m_
(2) Type section:
Pointer: P
Handle: h
Boolean: B
Floating point: f
No symbol: u
(3) Description section:
Initialization: init
Temporary variable: TMP
Purpose object: DST
Source object: SRC
Window: WND
Example below:
HWnd: h indicates the handle, the WND indicates the window, together into the "window handle".
m_bflag: m Represents member variables, b represents Boolean, together to: "A member variable, Boolean, is a state flag."