C# Event Naming Conventions: Best Practices and Examples

Do you want to know what are the event naming conventions in C#?

Here, I have explained the best practices and examples of C# event naming conventions.

Events in C# are a way to communicate between objects and indicate that something important has happened. Naming events properly is essential because it adds readability and semantic meaning to your code. The .NET Framework provides guidelines that help in naming events consistently.

Here is a summery of C# Event Naming Conventions:

Best PracticeGood ExampleBad Example
Use Pascal CasingItemSelecteditemSelected
Verb-Object FormatButtonClickedClickOfButton
ClarityUserAuthenticatedAuth
No ‘On’ PrefixConnectionLostOnConnectionLost
Descriptive NamesPaymentProcessedEv2
Avoid Data Type NamesTemperatureChangedIntEvent
Event-Argument PairingUserEventArgs paired with UserUpdatedMyEventArgs paired with UserUpdated

C# Event Naming Conventions

Let us check out the best practices of event naming conventions in C# below with examples.

Pascal Casing

In C#, event names should be in Pascal casing. This means the first letter of each word is capitalized. Here is an example you can see.

public event EventHandler ButtonClicked;

Verb-Object Format

The name should ideally be in a Verb-Object format to indicate an action. Here is an example:

public event EventHandler ItemSelected;

Clarity

The event name in C# should be clear, descriptive, and not abbreviated. Here is an example.

// Bad
public event EventHandler BtnClk;

// Good
public event EventHandler ButtonClicked;

No ‘On’ Prefix

Avoid using the ‘On’ prefix for event names. ‘On’ is commonly used to raise the event, not the event itself.

// Bad
public event EventHandler OnButtonClicked;

// Good
public event EventHandler ButtonClicked;

C# Event Naming Best Practices with Examples

Here are some best practices with examples.

1. Use Descriptive Names

Use a name that clearly indicates what the event signifies.

// Bad
public event EventHandler Ev1;

// Good
public event EventHandler UserAuthenticated;

2. Avoid Using Data Type Names

The name should not include data types or imply implementation details.

// Bad
public event EventHandler StringEvent;

// Good
public event EventHandler NameChanged;

3. Event-Argument Pairing

If you are creating custom event arguments, name them appropriately to pair with the event.

public event EventHandler<UserEventArgs> UserUpdated;

public class UserEventArgs : EventArgs
{
    // Implementation
}

And here are a few things you should avoid.

  1. Ambiguity: Avoid ambiguous event names like Error or Failed, as these don’t provide enough context.
  2. Redundancy: Don’t include redundant words like Event in the name, such as ClickEvent.
  3. Underscores: Never use underscores; always stick to Pascal casing.

Conclusion

I hope you got an idea of Event Naming Conventions in C#. I have also explained various best practices with examples.

You may also like: