4

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?

Dennis
  • 491
  • 5
  • 13
  • Similar: http://stackoverflow.com/questions/440163/textboxemployeename-vs-employeenametextbox – nawfal Jul 21 '13 at 10:45

2 Answers2

9

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.

BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
4

Microsoft is pretty explicit in what is appropriate for .Net naming conventions. Here's the detailed documentation:

http://msdn.microsoft.com/en-us/library/ms229002.aspx

Steven Behnke
  • 3,336
  • 3
  • 26
  • 34
  • Thanks, your answer helped me, too, but I really preferred the usage feedback from an experienced coder type of reply. – Dennis Jan 23 '09 at 15:34