Naming Convention in c# [closed]
Want to improve thi开发者_运维问答s question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this questionWhat is the universally accepted naming convention for c#? (functions, classes, parameters, local variables, namespaces, etc)
Microsoft has an excellent set of guidelines on class library design, including a section on naming. In short (examples in parentheses):
- Classes/Structs: PascalCase (
WebRequest
) - Interfaces: PascalCase with I prefix (
IDisposable
) - Methods: PascalCase (
ToUpper
) - Properties: PascalCase (
Length
) - Events: PascalCase (
Click
) - Namespaces: PascalCase (
System.Collections
; unusual to have two words in one part though) - Non-constant variables including parameters: camelCased (
keySelector
) - Constants: PascalCase (
Int32.MaxValue
) - Enums: PascalCase, singular for non-flags and plural for flags (
HttpStatusCode
,BindingFlags
) - Attributes: PascalCase with "Attribute" suffix (
ThreadStaticAttribute
)
Private names are up to you, but I tend to follow the same conventions as for everything else. Hungarian notation (in the style of Win32) is discouraged, although many places use "m_" or "_" as a prefix for instance variables.
Resharper's guidelines suggest
- Types and namespaces UpperCamelCase
- Interfaces IUpperCamelCase
- Type parameters TUpperCamelCase
- Methods properties and events UpperCamelCase
- Local variables lowerCamelCase
- Local constants lowerCamelCase
- Parameters lowerCamelCase
- Fields (not private) UpperCamelCase
- Instance fields (private) _lowerCamelCase
- Static field (private) _lowerCamelCase
- Constant fields (not private) UpperCamelCase
- Constant fields (private) UpperCamelCase
- Static readonly fields (not private) UpperCamelCase
- Static readonly fields (private) UpperCamelCase
- Enum members UpperCamelCase
- All other entities UpperCamelCase
The .NET standard from Microsoft is to use Pascal Case for namespaces, public and protected members (basically anything visible to other classes). For private members and local variables, there's a much wider berth to just do whatever you and your team are most comfortable with.
Don't underestimate the value of following the naming conventions of the platform you are working on as closely as possible.
Look at the reference material for the .NET Framework for examples of how to "fit in" (http://msdn.microsoft.com/en-us/library/ms229335.aspx).
Jon Skeet has given you a link to a good writeup by Microsoft: http://msdn.microsoft.com/en-us/library/ms229042.aspx
You can also use the standalone Microsoft FxCop (or Code Analysis if you have the Team Edition) http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&DisplayLang=en to check that the naming conventions have been followed. It has built-in rules for the Microsoft conventions, which is another reason you should be using them!
Juval Lowy took a stab at this is in Programming .NET Components, see this SO link too.
I'd have a look at the slim book called "Elements of C# Style" by Baldwin, Gray, & Misfeldt. The blue book covers naming conventions, and many other aspects of creating consistent, clean, readable code.
精彩评论