Lets say you have a button objects on a form with a click event (c#/.Net). How is the event handler normally named throughout the industry?
- fooButton_Click
- fooButtonClick
(or if you like Hungarian)
- btnFoo_Click
- btnFooClick
When visual studio creates the event handler it defaults to underscore + EventName.
This seems to violate the norm of using camelCasing and UppderCamelCasing instead of separating words with underscores.
Is there any reason to change it, or practically everyone following what Visual Studio does?
We are using well reviewed coding guidelines and standards which forbids the underscore for method (and event handlers) names. And, we are using PascalCase for method names.
Another practice for naming the event handlers is to give them time aware names. For example for the ‘Click’ event -> Click*ed*. This indicates that the event already happened and you have to deal with the rest. To make this name more time aware, the On prefix is being used. Last but not least, we are indicating the event object within the name, On*CancelButton*Clicked
OnCancelButtonClicked
would be my choice.
PS: The underscore for event handlers probably comes from Visual Basic (Classic) to Visual Studio.
3
I think there are places where the VS default is used, places where it is changed to some kind of different standard and places where it is up to what the individual developer prefers. Don’t expect to find any “industry norm”. Just find a consensus with your team how strict you want your coding standard to be, and if you want it strict, find some agreement how the code shall look.
The answer is in the language conventions for class library developers on MSDN.
That says that events should be PascalCase like everything else. The exceptions are parameters and protected field instances and that’s it.
Based on your example the name should be FooClick
or FooButtonClick
if the event source is FooButton
.
The specification advises developers not to use Hungarian notation at all.
2
FooButtonClick.
Start all method names, class names and properties with a capital letter.
But however which one you choose – be consistent with the choice. That’s the best advice I can give.