Naming Convention in c# [closed]

2019-01-06 09:52发布

What is the universally accepted naming convention for c#? (functions, classes, parameters, local variables, namespaces, etc)

6条回答
来,给爷笑一个
2楼-- · 2019-01-06 10:11

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.

查看更多
虎瘦雄心在
3楼-- · 2019-01-06 10:15

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.

查看更多
闹够了就滚
4楼-- · 2019-01-06 10:16

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!

查看更多
乱世女痞
5楼-- · 2019-01-06 10:26

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.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-06 10:31

Juval Lowy took a stab at this is in Programming .NET Components, see this SO link too.

查看更多
beautiful°
7楼-- · 2019-01-06 10:33

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
查看更多
登录 后发表回答