Naming Convention
-
I come from an MFC background where in general the hungarian naming convention is used: variables are preceded by a mnemonic representing their type, e.g. an int starts with 'n' - "nCount", a string starts with 'str' - "strName", etc. Also member variables in a class are preceded by 'm_'. Looking through the sample code in VS.NET it is obvious this kind of naming convention is not used in C# so what is generally used? Does Microsoft specify any standard?
-
I come from an MFC background where in general the hungarian naming convention is used: variables are preceded by a mnemonic representing their type, e.g. an int starts with 'n' - "nCount", a string starts with 'str' - "strName", etc. Also member variables in a class are preceded by 'm_'. Looking through the sample code in VS.NET it is obvious this kind of naming convention is not used in C# so what is generally used? Does Microsoft specify any standard?
I was reading somewhere recently that Microsoft no longer recommends nor uses Hungarian. "Thank you, thank you very much" Elvis.
-
I was reading somewhere recently that Microsoft no longer recommends nor uses Hungarian. "Thank you, thank you very much" Elvis.
-
I come from an MFC background where in general the hungarian naming convention is used: variables are preceded by a mnemonic representing their type, e.g. an int starts with 'n' - "nCount", a string starts with 'str' - "strName", etc. Also member variables in a class are preceded by 'm_'. Looking through the sample code in VS.NET it is obvious this kind of naming convention is not used in C# so what is generally used? Does Microsoft specify any standard?
MS doesn't suggest using hungarian notation anymore, instead they suggest using, um... I forget the word :-P For publicly accessable variables/properties/fields use
MyVariable
For private variables/properties/fields usemyVariable
[Camel casing?] Method names regardless of scope use MyMethod, classes the same MyClass. For exceptions end the exception name with Exception, attributes should end with Attribute. Thats all i can remember right now [not at my home PC]. HTH, James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002 -
MS doesn't suggest using hungarian notation anymore, instead they suggest using, um... I forget the word :-P For publicly accessable variables/properties/fields use
MyVariable
For private variables/properties/fields usemyVariable
[Camel casing?] Method names regardless of scope use MyMethod, classes the same MyClass. For exceptions end the exception name with Exception, attributes should end with Attribute. Thats all i can remember right now [not at my home PC]. HTH, James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002 -
Thanks for that. What about things like forms and form controls, I've seen prefixes of 'btn' used for Button controls and 'frm' used for Forms. Or is that just VB programmers who won't give up their bad habits? ;)
I think its VB programmers who don't give up bad habits. :) I've switched from lstItems for a listbox of Items to just plain Items if its the only one, or itemsLst (or even itemsList) if there is more than one 'items' object. James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002
-
I come from an MFC background where in general the hungarian naming convention is used: variables are preceded by a mnemonic representing their type, e.g. an int starts with 'n' - "nCount", a string starts with 'str' - "strName", etc. Also member variables in a class are preceded by 'm_'. Looking through the sample code in VS.NET it is obvious this kind of naming convention is not used in C# so what is generally used? Does Microsoft specify any standard?
I don't know about other peoples standards--this is what I use: private instance fields: _camelCasing private static fields: s_camelCasing public instance/static properties: MixedCase any methods: MixedCase any classes: MixedCase local variables: camelCasing local parameters: camelCasing controls on a form: _camelCasing (because after all, they are just private fields) Since there are no global functions or variables in C#, you can make various assumptions in your code. If you have the following snippet:
variable = "value";
Then you know that you are either have a local parameter to the current method, a local variable, an instance field/property, or a static field/property. Local parameters and local variables can be treated in a similar way (because they are all local to the current method). I use camel casing preceded with an underscore for private instance fields (so that I don't have to deal with name clashes). I precede static fields with "s_" for the same reason. The VS.NET IDE provides tooltips with detailed information about the variable/type that the mouse hovers over. Embedding the type into every variable name becomes somewhat redundent. -- Peter Stephens
-
MS doesn't suggest using hungarian notation anymore, instead they suggest using, um... I forget the word :-P For publicly accessable variables/properties/fields use
MyVariable
For private variables/properties/fields usemyVariable
[Camel casing?] Method names regardless of scope use MyMethod, classes the same MyClass. For exceptions end the exception name with Exception, attributes should end with Attribute. Thats all i can remember right now [not at my home PC]. HTH, James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002James T. Johnson wrote: For publicly accessable variables/properties/fields use MyVariable For private variables/properties/fields use myVariable [Camel casing?] Hmmm, I've gotten used to NEVER creating publicly accessable variables or fields, preferring instead to provide access to them via properties. Is that unecessarily innefficient do you think? Just another wannabe code junky
-
James T. Johnson wrote: For publicly accessable variables/properties/fields use MyVariable For private variables/properties/fields use myVariable [Camel casing?] Hmmm, I've gotten used to NEVER creating publicly accessable variables or fields, preferring instead to provide access to them via properties. Is that unecessarily innefficient do you think? Just another wannabe code junky
Senkwe Chanda wrote: Hmmm, I've gotten used to NEVER creating publicly accessable variables or fields, preferring instead to provide access to them via properties. Is that unecessarily innefficient do you think? Not at all, I prefer to use properties myself but I have used fields from time to time; mainly when I am making a readonly field in which case the data cannot change so there is no sense in making it a property (usually the reason for a property is so that data validation can be performed or a database lookup). Depending on how I plan on using it, I may make the values in a struct fields, but in most cases those are properties as well. James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972