Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WinForms Naming Convention

Tags:

c#

winforms

Is there any kind of naming convention for C# WinForms code? I Googled this a bit and got nowhere fast.

I'm designing a WinForms app and the Class View is becoming tough to navigate fast, so I need to apply some standards to find things faster.

  1. New (typical) control When you drag a new control onto a form in VS, it assigns it a camel cased name, but generates a private member field. I thought the convention for private class members was camel case prefixed with an underscore?

  2. What about events & methods for controls placed on a form? Is the Studio default of ControlName_MethodName the way to go for eveything?

like image 341
Dennis Avatar asked Dec 14 '22 05:12

Dennis


1 Answers

No, the convention in .NET is typically to not use underscores at all. There are exceptions like in the event handlers but for most things usually there are no underscores. Other than event handlers, I do not use underscores. I use camel casing for private members and functions and pascal casing for public members (properties) and functions. However, other people have different standards. Some people like to use the C++ convention of doing _someVariable or m_someVariable. I don't do this though.

I rarely use hungarian, but I do use hungarian for GUI controls (So a submit button might be btnSubmit, or a list box of customers would be lstCustomers).

As far as event handlers, you know, this is a bit of a gray area for me. If I use the Visual Studio "tab-complete" way of creating event handlers, I will typically leave the name as is. This, of course, introduces the underscores. However, if I don't, and it is not a GUI component, sometimes I just use the camelCasing. Probably for event handlers, it would be a good practice use the VS default naming scheme. I think it's fine as long as you stay relatively consistent.

like image 161
BobbyShaftoe Avatar answered Dec 31 '22 19:12

BobbyShaftoe