Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Convention in c# [closed]

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

like image 944
Nestor Avatar asked Oct 24 '09 15:10

Nestor


People also ask

What is naming conventions in C language?

Naming Conventions rules for Variables and Methods (Functions) are: It should begin with an alphabet. There may be more than one alphabet, but without any spaces between them. Digits may be used but only after alphabet. No special symbol can be used except the underscore (_) symbol.

Does C use Camelcase or Snakecase?

Classic C doesn't use camel-case; I've written code in camel-case in C, and it looks weird (so I don't do it like that any more). That said, it isn't wrong - and consistency is more important than which convention is used.

What is a name convention?

What Does Naming Convention Mean? Naming conventions are general rules applied when creating text scripts for software programming. They have many different purposes, such as adding clarity and uniformity to scripts, readability for third-party applications, and functionality in certain languages and applications.

What are the 3 rules for naming a variable?

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)


2 Answers

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.

like image 84
Jon Skeet Avatar answered Sep 29 '22 18:09

Jon Skeet


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
like image 37
DL Narasimhan Avatar answered Sep 29 '22 20:09

DL Narasimhan