Naming rules on msdn

Source: Internet
Author: User
Tags alphanumeric characters
Case sensitivity

Many naming conventions are related to the case sensitivity of identifiers. It is worth noting that the Common Language Runtime Library (CLR) supports case-sensitive and case-insensitive languages. The case sensitivity conventions described in this topic help developers understand and use libraries.

Case Sensitive Style

The following terms describe different cases of identifiers.

Pascal case

The first letter of the identifier and the first letter of each word connected to the end are capitalized. You can use Pascal case for a three-character or more-character identifier. For example:

Backcolor

Case sensitivity

The first letter of the identifier is lowercase, and the first letter of each connected word is uppercase. For example:

Backcolor

Uppercase

All letters in the identifier are in uppercase. For example:

Io

Case sensitivity rules for Identifiers

If an identifier is composed of multiple words, do not use separators between words, such as underscores (_) or hyphens. The case should be used to indicate the beginning of each word.

The following are general rules for identifiers.

For all public members, types, and namespace names composed of multiple words, use Pascal case.

Note that this rule does not apply to instance fields. Public instance fields should not be used because of the detailed description in the member design guidelines.

Use case-insensitive mixing for parameter names.

The following table summarizes the upper and lower case rules of identifiers and provides examples of different types of identifiers.

Identifier Case sensitivity Example

Class

Pascal

Appdomain

Enumeration type

Pascal

Errorlevel

Enumerated Value

Pascal

FatalError

Event

Pascal

Valuechanged

Exception

Pascal

Webexception

Read-only static Fields

Pascal

Redvalue

Interface

Pascal

Idisposable

Method

Pascal

Tostring

Namespace

Pascal

System. Drawing

Parameters

Camel

Typename

Attribute

Pascal

Backcolor

Uppercase/lowercase rules for acronyms

An acronym is a word that consists of the first letters of each word in a term or phrase. For example, HTML is the abbreviation of hypertext markup language. Acronyms should be used in identifiers only when the public is widely aware and understood. The acronym is different from the acronym because it is the abbreviation of a word. For example,IDYesIdentifier. Generally, the Library name should not use acronyms.

Note:

The two acronyms that can be used in the identifier areIDAndOK. In PASCAL case-sensitive identifiers, the uppercase and lowercase formats of the two acronyms areIDAndOK. If the two acronyms are used as the first word in a case-insensitive identifier, they are in the caseIDAndOK.

The case sensitivity of the first letter is determined by the length of the first letter. All acronyms must contain at least two characters. To facilitate the implementation of these principles, if a letter's acronym exactly contains two characters, it will be considered as a short-type acronym. The acronyms that contain three or more characters are long-form acronyms.

The following criteria specify the correct case-sensitive rules for short and long acronyms. The uppercase/lowercase rules of identifiers take precedence over those of acronyms.

The two characters must be capitalized, except when the first word of an acronym is used as an identifier in a case-insensitive format.

For exampleDbrateIs an identifier in Pascal's case-sensitive format. It uses short-form acronyms (DB) As the first word. For exampleIochannelThe parameter is an identifier in case-insensitive format (Io) As the first word.

Only the first character is capitalized, except when the first character is used as the first word of the case-insensitive identifier.

For exampleXmlwriterThe class is an identifier in Pascal's case-sensitive format. It uses the long-form acronyms as the first word. For exampleHtmlreaderThe parameter is an identifier in a mix of upper and lower cases. It uses a long-form acronym as the first word.

If an acronym starts with an identifier in a case-insensitive format, no character is capitalized regardless of the length of the acronym.

For exampleXmlstreamIs an identifier in both case and lower case format (XML) As the first word. For exampleDbservernameThe parameter is an identifier in case-insensitive format (DB) As the first word.

The case-sensitivity rules of Compound Words and common terms do not uppercase each word in the so-called compact format compound word. This composite refers to the composite word used to write a word, such as "endpoint ".

For example,HashtableIs a composite word in a compact format, which should be considered as a word and be case sensitive accordingly. If the Pascal case format is used, the compound word isHashtableThe compound word isHashtable. To determine whether a word is a composite word in a compact format, refer to the latest dictionary.

The following table lists some common terms that are not composite words in a compact format. The term is first displayed in Pascal's case-sensitive format, followed by a mix of uppercase and lowercase characters.

  • Bitflag(Bitflag)

  • Filename(Filename)

  • Logoff(Logoff)

  • Logon(Logon)

  • Signin(Signin)

  • Signout(Signout)

  • Username(Username)

  • Whitespace(Whitespace)

Case Sensitive

The case sensitivity criterion is used only to make the identifier easier to read and recognize. Case sensitivity rules cannot be used to prevent name conflicts between database elements.

Do not assume that all programming languages are case sensitive. This is not the case. Names cannot be case sensitive only. General naming conventions

The general naming convention discusses how to select the most appropriate name for the library element. These rules apply to all identifiers. The following sections discuss the naming of specific elements (such as namespaces or attributes.

Select an easy-to-read identifier. For example, the English attribute name horizontalalignment is more readable than alignmenthorizontal. Readability is more important than simplicity. The attribute name canscrollhorizontally is better than scrollablex (indicating the X axis, but not explicitly. Do not use underscores, hyphens, or any other non-alphanumeric characters. Do not use Hungarian notation.

Hungary notation uses a prefix to encode certain metadata of a parameter in an identifier, such as the Data Type of the identifier.

Avoid using identifiers that conflict with keywords in common programming languages.

Although CLS-compliant languages must provide methods to use keywords as common words, best practices do not require developers to understand how to implement them. For most programming languages, the language reference documentation provides a list of keywords used by the language. The following table provides links to reference documents for some common programming languages.

Language Link

C #

C # reference

C ++

C ++ Language Reference

Visual Basic

Visual Basic Reference

Abbreviations and acronyms

Generally, abbreviations or acronyms are not allowed. This type of name is less readable. Similarly, it is difficult to determine whether an acronym is widely recognized.

For more information about uppercase rules for abbreviations, see uppercase and lowercase rules for acronyms.

Do not use abbreviations or abbreviations as part of the identifier name.

For exampleOnbuttonclickDo not useOnbtnclick.

Do not use any unaccepted acronyms unless necessary. Language-specific names should use semantic-meaningful names for type names instead of language-specific keywords. For example, the name getlength is better than getint. In rare cases where the semantic meaning of an identifier is limited to its type, the common Common Language Runtime Library (CLR) type name should be used instead of the language-specific name.

For example, the method for converting data to int16 should be namedToint16InsteadToshortBecauseShortYesInt16Language-specific type name.

The following table shows the type names specific to the common programming language and CLR.

C # type name Visual Basic type name JScript type name Visual c ++ type name Ilasm.exe Representation CLR type name

Sbyte

Sbyte

Sbyte

Char

Int8

Sbyte

Byte

Byte

Byte

Unsigned char

Unsigned int8

Byte

Short

Short

Short

Short

Int16

Int16

Ushort

Uint16

Ushort

Unsigned short

Unsigned int16

Uint16

Int

Integer

Int

Int

Int32

Int32

Uint

Uint32

Uint

Unsigned int

Unsigned int32

Uint32

Long

Long

Long

_ Int64

Int64

Int64

Ulong

Uint64

Ulong

Unsigned _ int64

Unsigned int64

Uint64

Float

Single

Float

Float

Float32

Single

Double

Double

Double

Double

Float64

Double

Bool

Boolean

Boolean

Bool

Bool

Boolean

Char

Char

Char

Wchar_t

Char

Char

String

String

String

String

String

String

Object

Object

Object

Object

Object

Object

In rare cases where the identifier has no semantic meaning and the parameter type is not important, use a common name (such as a value or item) instead of repeating the type name. Name of the assembly and DLL

In most cases, an Assembly contains all or part of reusable libraries and is contained in a single Dynamic Linked library (DLL. An assembly can be split into multiple DLL files, but this is rare and is not described in this criterion.

The Assembly and DLL are the physical organizations of the library, while the namespace is the logical organization. Their composition should be independent of the Assembly's organization. Namespaces can and often span multiple sets.

Be sure to select the name indicating a large functional block (such as system. Data) for the Assembly DLL. The name of the assembly and DLL does not need to correspond to the namespace name, but it is reasonable to follow the namespace name method in the Assembly. Consider naming DLL in the following mode:

<Company>. <component>. dll

<Component> contains one or more clauses separated by dots.

For example,Contoso. webcontrols. dll.

Namespace name

The name selected for the namespace shall indicate the functions provided by the type in the namespace. For example, the system. net. Sockets namespace contains a type that allows developers to use sockets to communicate over the network.

The general format of the namespace name is as follows:

<Company>. (<product >|< technology>) [. <feature>] [. <subnamespace>]

For example,Microsoft. windowsmobile. DirectX.

Use the company name as the prefix of the namespace to prevent namespaces developed by different companies from having the same name and prefix. Use a stable version-independent product name at level 2 of the namespace name. Do not determine the name in the namespace hierarchy based on the organization hierarchy, because the company's department name may change after a period of time.

The namespace name is a long-used, unchangeable identifier. The development and changes of the Organization should not make the namespace name obsolete.

Use the Pascal case format and separate the namespace parts with periods (such as Microsoft. Office. PowerPoint ). If your brand uses a non-traditional case, it should follow the case defined by your brand, even if it is different from the common namespace case. When appropriate, consider using the plural namespace name. For example, use system. Collections instead of system. collection. However, brand names and acronyms are exceptions of this rule. For example, use system. Io instead of system. IOS. Do not use the same name for the namespace and its type. For example, do not use "debug" as the namespace name and provide a class named "debug" in the namespace. Some compilers require that this type be fully qualified. Namespace and type Name Conflict

If the selected namespace or type name conflicts with the existing name, the database user will have to restrict the reference of the affected items. This problem should not occur in most development scenarios.

Some of the guidelines provided in this section apply to the following namespace categories:

  • Application Model namespace

  • Basic namespace

  • Core namespace

  • Technical namespace Group

The namespace in the application model provides a function set specific to a class in the application. For example, the type in the system. Windows. Forms namespace provides the functions required to write Windows Forms client applications. The types in the system. Web namespace support compiling web-based server applications. Typically, namespaces in different application models are not used in the same application. Therefore, this reduces the possibility that name conflicts affect developers who use your library.

Infrastructure applications provide special support and are rarely referenced in program code. For example *.DesignerType in the namespace. *. PermissionsThe namespace is another example of the basic structure namespace. Conflicts with the type names in the basic schema namespace cannot affect the developers who use your library.

The core namespace isSystem.* Namespace (excluding the application namespace and basic namespace ). Both system and system. Text are examples of core namespaces. Avoid name conflicts with the types in the core namespace as much as possible.

The namespace that belongs to a specific technology will have the same first and second level identifier (Company. Technology .*). Avoid name conflicts in technical namespaces.

Generally, do not introduce a wide type name, such as element, node, log, and message. In general, this may lead to type name conflicts. You must specify a wide type name (such as formelement, xmlnode Eventlog, and soapmessage ). Do not specify the same name for multiple types in the namespace in a single application model.

For example, if you want to write a special control library to be used by a Windows Forms Application DeveloperCheckboxBecause the application model already has the same name type (checkbox ).

Do not specify a type name that will conflict with any type in the core namespace.

For example, do not useDirectoryAs the type name, because this will conflict with the directory type.

Do not assign a type name that will conflict with other types in a single technical namespace. Do not introduce the type name that will cause the technical namespace type to conflict with the type in the application model namespace (unless this technology is not used in the application model ). Class, structure, and interface Name

Generally, a type name should be a noun phrase, where a noun is an entity represented by a type. For example, buttons, stacks, and files all have names, which are used to identify objects represented by types. Select the name of the entity from the developer's perspective. The name should reflect the usage.

The following guidelines apply to selecting a type name.

Use nouns or noun phrases (or occasionally use adjectives) to name classes, interfaces, and value types in Pascal case format. Do not add a prefix (such as the letter C) to the class name ).

This rule is not applicable to the interface. It should contain lettersI.

Consider using the base class name at the end of the derived class.

For example, the framework type inherited from stream isStreamAt the end, the type inherited from exception isExceptionEnd.

The interface name is prefixed with the letter I to indicate that this type is an interface. When defining a class/interface pair (where the class is the standard implementation of the Interface), make sure that the class and interface names should be identical except the interface names with the I prefix.

For example, the framework provides the iasyncresult interface and the asyncresult class.

Generic parameter name

Generic is a major new feature of. NET Framework 2.0. The following guidelines apply to selecting the correct name for a generic type parameter.

Use descriptive names for generic type parameters, unless the names of a single letter are completely self-explanatory and do not require descriptive names.

Idictionary is an example of an interface that complies with this criterion.

For a type with a single letter type parameter, use the letter T as the type parameter name of these types. Use the letter T as the prefix of the descriptive type parameter name. Consider specifying the constraints placed on the Type parameter in the parameter name. For example, the parameter restricted by isession can be called tsession. Common Types

The naming conventions provided by the following guidelines help developers understand the usage of certain classes. The inherited types mentioned in the criterion refer to all successors, not just directly inherited types. For exampleExceptionAdd inherited typesExceptionSuffix "indicates thatExceptionAny typeExceptionThe name at the end.

Each such criterion is also used to retain the specified suffix. This suffix should not be used unless the type meets the conditions described in this criterion. For example, if the type is not fromExceptionIf it is directly or indirectly inherited, the type name cannot beExceptionEnd.

Add the attribute suffix to the custom attribute class.

Obsoleteattribute and attributeusageattribute are type names that comply with this criterion.

Add the eventhandler suffix to the name of the type used in the event (such as the return type of the C # event.

Assemblyloadeventhandler is the name of the delegate that complies with this criterion.

Add a callback suffix to the name of the delegate that is not an event handler. Do not add the delegate suffix to the delegate. Add the suffix eventargs to the class that extends system. eventargs. Do not derive from the system. Enum class; Use the keywords supported by the current language. For example, the enum keyword should be used in C. Add the exception suffix to the type inherited from system. Exception. Add a dictionary suffix to the type that implements system. Collections. idictionary or system. Collections. Generic. idictionary <tkey, tvalue>. Note that system. Collections. idictionary is a set of specific types, but the priority of this criterion is higher than the following more general set criterion. To implement system. collections. ienumerable, system. collections. icollection, system. collections. ilist, system. collections. generic. ienumerable <t>, system. collections. generic. icollection <t> or system. collections. generic. the collection suffix is added to the ilist <t> type. Adds a stream suffix to the type inherited from system. Io. stream. Add a permission suffix to the type inherited from system. Security. codeaccesspermission or the type implementing system. Security. ipermission. Do not use the prefix in the enumerated Value Name. For example, do not use a prefix such as ad for ADO enumeration, or use a prefix such as RTF for multi-format text enumeration.

This also means that the enumerated value name should not contain the enumerated type name. The following code example demonstrates incorrect enumeration value naming.

C # copy code
public  enum Teams{TeamsAlpha,TeamsBeta,TeamsDelta}
Do not use Enum as the suffix of the enumeration type. Do not add flags as the Suffix in the name of the Flag enumeration. Use a singular name for enumeration unless the enumerated value is a bit field. Use the plural name for the enumeration (also called the flag enumeration) that uses the bitfield value.

Type member name

Types include the following members:

  • Method

  • Attribute

  • Field

  • Event

The guidelines in this section help the class library designer to select names consistent with. NET Framework for members.

The method name uses a verb or verb phrase as the method name.

Generally, Methods perform operations on data. Therefore, operations that use verb descriptions make it easier for developers to understand the operations performed by methods. When defining operations performed by methods, you should carefully select a specific name from the developer's perspective. Do not select a verb that describes how a method performs its operations, that is, do not use implementation details as the method name.

The attribute name uses nouns, noun phrases, or adjectives as the attribute name.

Noun phrases or adjectives are suitable for attributes because attributes store data.

Do not use an attribute with the same name as the get method.

For example, do not name an attributeEmployeerecord, And name a methodGetemployeerecord. Developers do not know which member to use to complete their programming tasks.

Use a positive phrase as the name of a Boolean attribute (for example, use canseek instead of cantseek ). In addition, you can add a prefix (such as is, can, or has) to a Boolean attribute, but be sure to use it properly. Provide the same name as the property type.

If an attribute has been forcibly typed into an enumeration, the attribute can have the same name as the enumeration. For exampleCachelevel, The property of one of the returned enumerated values can also be namedCachelevel.

The event name uses a verb or verb phrase as the event name. When naming an event, use the current or past tense to indicate the concepts of time. For example, the closing event that is triggered before the window is closed can be named "Closing", and the closing event that is triggered after the window is closed can be named "closed ". Do not use "before" or "after" as the prefix or suffix to indicate previous and subsequent events. Use the suffix eventhandler to name the event handler (used as the delegate of the event type ). Use two parameters named "sender" and "e" in the event handler signature.

SenderThe parameter type should be object,EThe parameter must be an eventargs instance or be inherited fromEventargs.

Use the eventargs suffix to name the event parameter class. Field name

The field naming rules apply to static public fields and static protected fields. Do not define public instance fields or protected instance fields. For more information, see field design.

Use the Pascal case format in the field name. Use a noun or noun phrase as the field name. Do not use the prefix in the field name. For example, do not use g _ or S _ to distinguish static and non-static fields.

Parameter Name

Selecting an appropriate parameter name can greatly improve the database availability. The appropriate parameter name should indicate the data or functions that will be affected by this parameter.

Use camel case for parameter names. Use descriptive parameter names.

In most cases, the parameter name and its type should be sufficient to determine the parameter usage.

Consider using a name that reflects the meaning of a parameter rather than a name that reflects the parameter type.

In developer tools and documents, parameter types are usually visible. By selecting a name that describes the usage or meaning of a parameter, You can provide valuable information to developers, help them find the members required for the task, and help them transmit correct data to members.

Resource Name

The guidelines in this topic apply to localized resources, such as error messages and menu text.

Use the Pascal case format in the Resource Key. Provide descriptive identifiers instead of short identifiers. Keep identifiers concise, but do not sacrifice readability. Do not use language-specific keywords in the Common Language Runtime Library (CLR) programming language. Only letters, numbers, and underscores can be used in naming resources. The dot separator (".") is used to represent the identifier in a clear hierarchy.

For example,Menus. filemenu. Close. TextAndMenus. filemenu. Close. ColorAnd so on.

Use the following naming conventions for exception message resources. A resource identifier consists of an exception type name and an exception short identifier, which are separated by points.

For example,Argumentexception. badenumvalueMeets this criterion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.