5

I am struggling with naming convetions in C# I do not know when to use PascalCase or camelCase. I have read Microsoft's naming convention, but I got lost ther http://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx

As far as I know this is correct:

public int Number;
private int number;
public void Method();
private void method();
int number;//for any local variable

But in context of that above how should I name components in XAML: x:Name="Button" or x:Name="button". They seems to be private field so I should use camelCase, but on the other hand everywhere on the examples I see PascalCase for x:Name

I do not ask about one's habits, but orthodoxic/official/C# guru style.

Shiva
  • 205
  • 3
  • 10
Yoda
  • 179
  • 1
  • 1
  • 7
  • @Carson63000 In this link there is lot of talking, no examples. – Yoda Jan 21 '14 at 02:25
  • 4
    Lower case private methods are **not** part of official Microsoft naming convention. Personally I have never seen a C# project that use such casing. I'm not saying noone ever does, but it's kind of exotic and not recommmended by MS. – Konrad Morawski Jan 21 '14 at 11:36
  • 2
    The framework conventions only apply to externally visible types/members (visibility is `public` or `protected`). For the rest you can use project/company specific conventions. – CodesInChaos Jan 21 '14 at 16:06

2 Answers2

15

The conventions I use are

//non-private variable
variableName

//private variable
_variableName

//methods
MethodName

//parameters
parameterName

//Properties
PropertyName

These are standard MS c# conventions. There is a list here. If you want something to help you keep nice clean code following conventions, consider Resharper add-on for Visual Studio

Lotok
  • 1,759
  • 2
  • 17
  • 27
  • 3
    The underscore as the (private) field prefix is a controversial one. Personally I use it, but it has a suspicious taste to me and I don't follow this convention in all projects. – Konrad Morawski Jan 21 '14 at 11:32
  • Yeah, it does seem like a controversial subject. It is [discussed briefly on MSDN.](http://social.msdn.microsoft.com/Forums/vstudio/en-US/044a1246-0a15-48d5-902c-9c6c462af8b2/when-to-prefix-a-variable-with-underscore?forum=csharpgeneral) It appears the convention originated in c/c++ – Lotok Jan 21 '14 at 11:36
4

I read the MS standards, a long time ago. All I remember and use is:

  • UpperCamelCase for public methods and properties
  • lowerCamelCase for protected/private methods and properties, also parameters

At the time, there was no XAML, but I don't expect precise consistency across languages.

In the end, no one's standards matter but your own. Or your team's. Or your boss's. But mostly your own.

Steven A. Lowe
  • 33,808
  • 2
  • 84
  • 151
  • 1
    I would mostly agree with this answer, with the caveat that anything public should follow the idioms and conventions of the language. Since a C# developer is going to expect to find VerbNoun methods (like `ToString()` or `ExecuteNonQuery()`), you should both name and capitalize your methods to meet their expectations. – mgw854 Jan 21 '14 at 04:32
  • 5
    Lower case for private methods?? Not in their official guidelines http://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx it isn't. I have also never seen such convention in open-source projects maintained by Microsoft (like ASP MVC or Entity Framework). – Konrad Morawski Jan 21 '14 at 11:26
  • 1
    @KonradMorawski: I haven't read the conventions in about a decade, thanks for the update. – Steven A. Lowe Jan 21 '14 at 18:47
  • Isn't "UpperCamelCase" better known as PascalCase? – Craig W Jan 21 '14 at 19:37
  • @CraigW: "better known" is subjective ;) – Steven A. Lowe Jan 21 '14 at 19:41
  • @StevenA.Lowe I [disagree](http://www.googlefight.com/index.php?word1=PascalCase&word2=UpperCamelCase). ;-) – Craig W Jan 21 '14 at 19:45
  • 1
    @CraigW: bwahahahahaha! awesome! – Steven A. Lowe Jan 21 '14 at 21:35
  • 2
    "no one's standards matter but your own" That's what I will tell my team the next time they ask me why I write everything in CAPS. – Orestis P. Sep 02 '16 at 05:25