大小寫慣例Capitalization Conventions

簡單的方法,使用這一章配置中的指導方針情況下,當套用一致的方式,請識別項型別、 成員和參數容易閱讀。The guidelines in this chapter lay out a simple method for using case that, when applied consistently, make identifiers for types, members, and parameters easy to read.

識別項的大小寫規則Capitalization Rules for Identifiers

若要區分識別項中的文字,利用識別項中的每個單字的第一個字母。To differentiate words in an identifier, capitalize the first letter of each word in the identifier. 請勿使用底線來區分文字,或就此而言,在識別項中任何位置。Do not use underscores to differentiate words, or for that matter, anywhere in identifiers. 有兩個適當的方法,可以改為大寫識別碼,根據所使用的識別項:There are two appropriate ways to capitalize identifiers, depending on the use of the identifier:

  • PascalCasingPascalCasing

  • camelCasingcamelCasing

參數名稱除外的所有識別項所使用的 PascalCasing 慣例轉換成大寫字母 (包括透過長度的兩個字母的縮寫) 的每個字的第一個字元,如下列範例所示:The PascalCasing convention, used for all identifiers except parameter names, capitalizes the first character of each word (including acronyms over two letters in length), as shown in the following examples:

PropertyDescriptor
HtmlTag

特殊案例是進行中這兩個字母大寫,兩個字母縮寫,下列識別碼中所示:A special case is made for two-letter acronyms in which both letters are capitalized, as shown in the following identifier:

IOStream

CamelCasing 慣例,參數名稱,僅用於轉換成大寫字母的第一個字,除了每個單字的第一個字元,如下列範例所示。The camelCasing convention, used only for parameter names, capitalizes the first character of each word except the first word, as shown in the following examples. 因為此範例也示範,開始使用依照 camel 命名法大小寫的識別項的兩個字母縮略字都是小寫。As the example also shows, two-letter acronyms that begin a camel-cased identifier are both lowercase.

propertyDescriptor
ioStream
htmlTag

✓ 請 為所有由多個單字所組成的公用成員、 類型和命名空間名稱使用 PascalCasing。✓ DO use PascalCasing for all public member, type, and namespace names consisting of multiple words.

✓ 請 為參數名稱使用 camelCasing。 ✓ DO use camelCasing for parameter names.

下表描述不同類型的識別項的大小寫規則。The following table describes the capitalization rules for different types of identifiers.

識別項Identifier 大小寫Casing 範例Example
命名空間Namespace Pascal 命名法Pascal namespace System.Security { ... }
類型Type Pascal 命名法Pascal public class StreamReader { ... }
介面Interface Pascal 命名法Pascal public interface IEnumerable { ... }
方法Method Pascal 命名法Pascal public class Object {
public virtual string ToString();
}
屬性Property Pascal 命名法Pascal public class String {
public int Length { get; }
}
Event - 事件Event Pascal 命名法Pascal public class Process {
public event EventHandler Exited;
}
欄位Field Pascal 命名法Pascal public class MessageQueue {
public static readonly TimeSpan
InfiniteTimeout;
}
public struct UInt32 {
public const Min = 0;
}
列舉值Enum value Pascal 命名法Pascal public enum FileMode {
Append,
...
}
參數Parameter 依照 camel 命名法Camel public class Convert {
public static int ToInt32(string value);
}

大寫的複合字和一般詞彙Capitalizing Compound Words and Common Terms

大部分的複合詞彙會被視為單一字詞大小寫的目的。Most compound terms are treated as single words for purposes of capitalization.

X DO NOT 所謂的封閉式複合字組中的每個字大寫。X DO NOT capitalize each word in so-called closed-form compound words.

這些是寫成單字,例如端點的複合字。These are compound words written as a single word, such as endpoint. 大小寫的指導方針,以將關閉表單的複合字視為單一的文字。For the purpose of casing guidelines, treat a closed-form compound word as a single word. 使用目前的字典來判斷複合字以關閉表單。Use a current dictionary to determine if a compound word is written in closed form.

Pascal 命名法Pascal 依照 camel 命名法Camel notNot
BitFlag bitFlag Bitflag
Callback callback CallBack
Canceled canceled Cancelled
DoNot doNot Don't
Email email EMail
Endpoint endpoint EndPoint
FileName fileName Filename
Gridline gridline GridLine
Hashtable hashtable HashTable
Id id ID
Indexes indexes Indices
LogOff logOff LogOut
LogOn logOn LogIn
Metadata metadata MetaData, metaData
Multipanel multipanel MultiPanel
Multiview multiview MultiView
Namespace namespace NameSpace
Ok ok OK
Pi pi PI
Placeholder placeholder PlaceHolder
SignIn signIn SignOn
SignOut signOut SignOff
UserName userName Username
WhiteSpace whiteSpace Whitespace
Writable writable Writeable

區分大小寫Case Sensitivity

雖然有些可以在 CLR 執行的語言不需要支援區分大小寫。Languages that can run on the CLR are not required to support case-sensitivity, although some do. 即使您的語言支援,可能會存取您的架構的其他語言則否。Even if your language supports it, other languages that might access your framework do not. 任何 Api,從外部存取,因此,不能依賴來區別兩個名稱相同的內容中的案例。Any APIs that are externally accessible, therefore, cannot rely on case alone to distinguish between two names in the same context.

X DO NOT 假設所有的程式語言不區分大小寫。X DO NOT assume that all programming languages are case sensitive. 但它們並不相等。They are not. 名稱不能單獨的大小寫不同。Names cannot differ by case alone.

Portions © 2005, 2009 Microsoft Corporation.All rights reserved.Portions © 2005, 2009 Microsoft Corporation. All rights reserved.

皮耳森教育,inc.的權限所印製Framework 設計方針:慣例、 慣用句和可重複使用的.NET 程式庫,第 2 版的模式Krzysztof Cwalina 和 Brad Abrams,2008 年 10 月 22 日由 Addison-wesley Professional 的 Microsoft Windows 開發系列的一部分發行。Reprinted by permission of Pearson Education, Inc. from Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition by Krzysztof Cwalina and Brad Abrams, published Oct 22, 2008 by Addison-Wesley Professional as part of the Microsoft Windows Development Series.

另請參閱See also