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 Practice | Good Example | Bad Example |
---|---|---|
Use Pascal Casing | ItemSelected | itemSelected |
Verb-Object Format | ButtonClicked | ClickOfButton |
Clarity | UserAuthenticated | Auth |
No ‘On’ Prefix | ConnectionLost | OnConnectionLost |
Descriptive Names | PaymentProcessed | Ev2 |
Avoid Data Type Names | TemperatureChanged | IntEvent |
Event-Argument Pairing | UserEventArgs paired with UserUpdated | MyEventArgs 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.
- Ambiguity: Avoid ambiguous event names like
Error
orFailed
, as these don’t provide enough context. - Redundancy: Don’t include redundant words like
Event
in the name, such asClickEvent
. - 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:
- C#.NET Variable Naming Conventions
- C#.Net array naming conventions with examples
- C# dictionary naming conventions
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…