Flylib.com

Books Software

 
 
 

C.2 Naming C events


C.2 Naming C# events

Delegate names which are to be used for declaring events (event handlers) should end with EventHandler to differentiate them from other delegates. If your event handler takes in the standard reference to sender/event source and event argument object parameters, use the variable names sender and e . For example:

delegate void Mouse

EventHandler

(object

sender

, MouseEvent

e

);

Event argument class names should end with EventArgs . For example:

public class My

EventArgs

: EventArgs{...}

C.3 Naming C# enums

Use Pascal casing for both enum identifiers and enum value names . For example:

enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

It is not recommended to add Enum after the enum identifier. In the case above, Day would be more appropriate than DayEnum .

Enum names should be singular “ Day is preferable to Days (it is obvious that an enum type will represent multiple items).


C.4 Naming C# interfaces

Interface identifiers should start with an ' I '. For example, ISerializable is preferable to Serializable as an interface name . Do not separate the ' I ' from the remaining characters with an underscore ISerializable is preferable to I_Serializable .

Unlike class identifiers (which should be noun phrases), interface identifiers can be either noun phrases or adjectives (phrases describing behavior). Examples of suitable adjectives include IFormatable and ISerializable .

It is okay to name interface/class pairs (a class that is meant to implement an interface) so that their names differ only by the ' I ' suffix, especially if a noun phrase has been selected for the interface name. For example, the IComponent interface is expected to be implemented by the Component class.


C.5 Naming C# properties

If you choose to use the same name for a public or protected property which 'represents' a private field in a C# class, they should be differentiated by capitalization. The public/protected property name should use Pascal casing, while the private field should use camel casing . Here is an example:

1:  private string

name

; // private field name
 2:
 3:  public string

Name

{ // property Name
 4:    get{
 5:      return name;
 6:    }
 7:    set {
 8:      name = value;
 9:    }
10:  }

C.6 Naming namespaces

Like Java package names , C# namespaces should be unique so that the chance of another development team somewhere across the globe choosing an identical name is extremely small.

While Java recommends that you name your Java packages using your company's allocated domain name on the world wide web, Microsoft recommends that you name your C# namespaces like this:

<CompanyName>.<TechnologyName>

For example, a class written by Addison-Wesley's technical department can be placed into the AddisonWesley.TechDept namespace. TechDept can be further divided into multiple namespaces based on the project title.

Additional notes

  • Use plural nouns where appropriate. For example, AddisonWesley. TechDept.SharedUtilities is preferable to AddisonWesley.TechDept. SharedUtility .

  • Avoid giving a class the same name as a namespace. If you already have a SharedUtility namespace, try not to have a class of that name.

  • In the same way that you should avoid putting your Java classes into the java.lang package, you should also avoid using the System namespace for your own classes, even though nothing prevents you from doing so. Other namespace identifiers to avoid include words like Collections , Forms , and UI .