4

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.

  • @Carson63000 In this link there is lot of talking, no examples. – Yoda Jan 21 '14 at 2: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
13

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

  • 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. It appears the convention originated in c/c++ – James Jan 21 '14 at 11:36
3

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.

  • 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 4:32
  • 4
    Lower case for private methods?? Not in their official guidelines 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
  • 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 2 '16 at 5:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.