Microsoft .NET Coding Standards [微软官方命名规范]
Microsoft .NET Coding Standards
1. Overview
This document applies to all .NET compliant languages since any .NET language will utilize the same class library, have the same responsibilities for exception handling and threading, etc. This document outlines the coding convention that should be used, and high lights some key design guidelines as defined by Microsoft. Please refer to the Microsoft Design Guidelines for Class Library Developers [1] for a comprehensive guide on how to write reusable, robust, and scalable components.
2. Coding Conventions
Applying coding conventions in .NET involves the three classical naming schemas: Pascal Casing, Camel Casing, and Hungarian Notation.
Pascal Casing – Capitalize the first letter of every word, e.g. MyClassName.
Camel Casing – Capitalize the first letter of every word except the first word, e.g. myVariableName.
Hungarian Notation – Hungarian notation involves including a prefix of the data type as well as a prefix to denote the scope of the variable, e.g. mstrMyString (m stands for “member variable/field in a class” and “str” indicates that the type is a string).
2.1 Namespace
Pascal casing used for all namespaces. Namespaces that contain functionality that can be reused in other projects should be prefixed with OpenCourse, for example, the namespace OpenCourse.Web.CustomControls.TabControl could be used to describe a custom ASP.NET control named “TabControl.”
2.2 Class
Pascal casing is used for all class names. Class names should be descriptive and abbreviations should be avoided. The underscore character should be avoided as well.
2.3 Interface
Pascal casing is used for all interfaces, and all interfaces shall be prefixed with capital letter “I” to denote that it is an interface. Abbreviations and use of the underscore character should be avoided.
2.4 Method
Pascal casing is used for all methods. It is recommended to use a verb or a verb phrase to describe a method since a method inevitable performs some functionality on the object. Abbreviations and use of the underscore character should be avoided.
2.5 Parameter
Parameter names should be descriptive and use Camel casing.
2.6 Property
Property should be descriptive and use Pascal casing.
2.7 Field
Field names (data members that are members of a class) should use Hungarian notation and use the prefix “m” to denote that the field is a variable that is a member of a class. Example:
VB.NET
Class MyClass
Private mstrMyStringValue As String
End Class
C#
Class MyClass
{
private string mstrMyStringValue;
}
2.8 Event Handlers
An event is a notification sent by an object and this notification can be detected and maybe handled. A verb or gerund is used in naming event handlers. The event name should be suffixed with 'EventHandler'. By convention the calling object should be passed as a parameter along with a parameter for the state of the event.
VB.NET
Public Delegate Sub ScreenEventHandler(sender As Object, e As
ScreenEventArgs)
C#
public delegate void ScreenEventHandler(object sender, ScreenEventArgs e);
2.9 Commenting
At the very minimum, comments must be provided in the .NET XML comment format for all classes, methods, properties, and fields.
3. Design Guidelines
Please refer to the Microsoft Design Guidelines for Class Library Developers [1] for a comprehensive set of design guidelines.
3.1 Exception handling
- Refrain from directly throwing the standard ApplicationException or Exception exceptions. Instead, create a set of exception classes that derive from ApplicationException and use them to throw exceptions generated in the class library. The more specific exceptions, like FileNotFoundException, can be thrown if the correctly convey the type of error.
- Do not use exceptions for normal flow of control. For very common error situations, return null instead of throwing an exception.
- If invalid parameters are passed to a method, always throw an InvalidArgumentException.
- Use localized strings to describe the error.
- If an exception is thrown, clean up what has already been performed in the function. The caller who catches the exception will assume that since an exception was thrown, no work was performed.
3.2 Database Access (ADO.NET)
- Use the built-in .NET connection pooling features. Open a connection for every call, and remember to close the connection. Use a try-finally pattern to ensure that the connection is always closed regardless of if an exception was thrown or not.
- Avoid using the automatic update/delete/insert functionality of the DataSet and CommandBuilder classes for anything but prototype code.
- Use care when using any of the Reader classes (SqlDataReader, OleDbDataReader, OdbcDataReader) since the reader instance will stay open on the connection until explicitly closed.
附:非官方版的数据类型简写
数据类型 |
数据类型简写 |
标准命名举例 |
Array |
arr |
arrShoppingList |
Boolean | bln | blnIsPostBack |
Byte | byt | bytPixelValue |
Char | chr | chrDelimiter |
DateTime | dtm | dtmStartDate |
Decimal | dec | decAverageHeight |
Double | dbl | dblSizeofUniverse |
Integer | int | intRowCounter |
Long |
lng |
lngBillGatesIncome |
Object | obj | objReturnValue |
Short | shr | shrAverage |
Single | sng | sngMaximum |
String |
str |
strFirstName |