| title | description | ms.date | helpviewer_keywords | ms.assetid | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Names of Type Members |
Learn the guidelines for naming type members in .NET, such as methods, properties, events, and fields. |
10/22/2008 |
|
af5a0903-36af-4c2a-b848-cf959affeaa5 |
Names of Type Members
Types are made of members: methods, properties, events, constructors, and fields. The following sections describe guidelines for naming type members.
Names of Methods
Because methods are the means of taking action, the design guidelines require that method names be verbs or verb phrases. Following this guideline also serves to distinguish method names from property and type names, which are noun or adjective phrases.
public class String {
public int CompareTo(...);
public string[] Split(...);
public string Trim();
}Names of Properties
Unlike other members, properties should be given noun phrase or adjective names. That is because a property refers to data, and the name of the property reflects that. PascalCasing is always used for property names.
public string TextWriter { get {...} set {...} }
public string GetTextWriter(int value) { ... }
This pattern typically indicates that the property should really be a method.
CanSeek instead of CantSeek). Optionally, you can also prefix Boolean properties with "Is", "Can", or "Has", but only where it adds value.
For example, the following property correctly gets and sets an enum value named Color, so the property is named Color:
public enum Color {...}
public class Control {
public Color Color { get {...} set {...} }
}Names of Events
Events always refer to some action, either one that is happening or one that has occurred. Therefore, as with methods, events are named with verbs, and verb tense is used to indicate the time when the event is raised.
Examples include Clicked, Painting, DroppedDown, and so on.
For example, a close event that is raised before a window is closed would be called Closing, and one that is raised after the window is closed would be called Closed.
public delegate void ClickedEventHandler(object sender, ClickedEventArgs e);
sender and e in event handlers.
The sender parameter represents the object that raised the event. The sender parameter is typically of type object, even if it is possible to employ a more specific type.
Names of Fields
The field-naming guidelines apply to static public and protected fields. Internal and private fields are not covered by guidelines, and public or protected instance fields are not allowed by the member design guidelines.
For example, do not use "g_" or "s_" to indicate static fields.
Portions © 2005, 2009 Microsoft Corporation. All rights reserved.
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.