14

I recently started working with Unity3D and primarily scripting with C#. As I normally program in Java, the differences aren't too great but I still referred to a crash course just to make sure I am on the right track.

However, my biggest curiosity with C# is that it capitalises the first letter of its method names (eg. Java: getPrime() C#: GetPrime() aka: Pascal Case?). Is there a good reason for this? I understand from the crash course page that I read that apparently it's convention for .Net and I have no way of ever changing it, but I am curious to hear why it was done like this as opposed to the normal (relative?) camel case that, say, Java uses.

Note: I understand that languages have their own coding conventions (Python methods are all lower case which also applies in this question) but I've never really understood why it isn't formalised into a standard.

Thomas W.
  • 107
  • 5
ahodder
  • 791
  • 3
  • 8
  • 16
  • 18
    I really don't think you can look at `camelCase` and `PascalCase` and `underscore_case` and say that one of them is normal (even relatively normal) and the others not. As @dasblinkenlight said, it's an arbitrary choice. The only thing making you think that C#'s convention unusual is that you "normally program in Java", and are thus accustomed to the arbitrary choice made for Java. – Carson63000 Aug 02 '12 at 05:00
  • Use JavaScript instead.. for Unity3D :) – Lipis Aug 07 '12 at 00:54
  • @Lipis Why for - other than personal taste? ;-) – ahodder Aug 07 '12 at 14:58
  • @AedonEtLIRA totally personal.. never mind :) Enjoy Unity no matter what language.. you'll end up using.. – Lipis Aug 07 '12 at 22:13
  • @Lipis So far it's great. Tons of bells and whistles and I haven't yet dropped money on it yet. But I do prefer the structured style of C# (c/c++) and java. – ahodder Aug 07 '12 at 22:29
  • @AedonEtLIRA http://answers.unity3d.com too bad they don't have the SE engine (they used to).. but is still I think a good place to find answers... the community is very active.. – Lipis Aug 07 '12 at 23:56
  • C# isn't "scripting." ;-) It's a compiled high level computer language. – Craig Tullis Jun 22 '16 at 22:56
  • ProperCase method names vs camelCase field names helps make a distinction between methods and fields when you're glancing at code. – Craig Tullis Jun 22 '16 at 22:59
  • @Carson63000 Casing with underscores and lowercase is called `snake_case` because it "keeps low" like a snake, and it's what Python uses, and pythons are snakes. – Aaron Franke Jun 11 '19 at 09:16

3 Answers3

27

Naming conventions represent arbitrary choices of their publisher. There is nothing in the language itself to prohibit you from naming your methods the way you do in Java: as long as the first character is a letter/underscore, and all other characters are letters, digits, or underscores, C# is not going to complain. However, the class libraries that come with .NET follow a convention that Microsoft has adopted internally. Microsoft also published these guidelines, so that others may choose to adopt them for their own class libraries too. Although it is your choice to follow or to ignore Microsoft's guidelines, familiarization with your code by others may go faster if you follow the same naming guidelines.

Sergey Kalinichenko
  • 17,393
  • 4
  • 57
  • 73
  • 1
    However there is usually some rationale behind a convention, and when I read "Is there a good reason for this?" I understand it as a question about WHY not WHAT. – greenoldman Oct 08 '13 at 09:42
  • Pascal for public/internal/static members and all methods. Camel for private. It helps distinguish the scope of a token while you scan it. The `this` keyword does the same. The style choices are practical, even if they may appear alien – Gusdor Dec 26 '16 at 11:16
23

Perhaps due to Pascal / Delphi influence. The creator of C# and Delphi was the same person after all (Anders Hejlsberg).

Delphi coding conventions by and large happen to be the same as C#'s in this aspect; see http://www.econos.de/delphi/cs.html#ObjectPascal_Procedures or http://wiki.delphi-jedi.org/index.php?title=Style_Guide#Method_Naming - coincidence?

Thomas W.
  • 107
  • 5
Konrad Morawski
  • 9,721
  • 4
  • 37
  • 58
4

In addition to the other answers made, having camel case for methods means that they can conflict with names for private members, parameters and method variables that use camel case for their naming. In Java (and C# 1.0) this isn't terribly common since delegate use is awkward and rare. In modern C# it's not exactly common, but it's also not unheard-of.

Telastyn
  • 108,850
  • 29
  • 239
  • 365
  • C# and Delphi (whose creator is the same) both are case-INsensitive and thus can never have collisions with stuff that only differs in casing. Both therefore also have conventions for naming private member variables. In Delphi they start with F, in C# with an underscore. Camel cased method names therefore never conflict with (private) member variable names. Plus there is never any ambiguity about whether you are dealing with a var or a method. – Marjan Venema Aug 02 '12 at 06:10
  • 1
    The underscore should not be used in C# private fields (see http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx, e.g), but it's controversial and often found. – Jens Aug 02 '12 at 07:05
  • 1
    relying on casing to distinguish between language constructs is a really bad idea, regardless of whether the language allows you to do it or not. – gbjbaanb Aug 02 '12 at 10:11
  • I have never, in a decade of professional C# work seen underscores used for private members. – Telastyn Aug 02 '12 at 11:03
  • 4
    @Telastyn that statement is pretty shocking. Most of the .NET class library uses that convention. – MattDavey Aug 02 '12 at 14:31
  • @MattDavey:Your example is the exact reason why Telastyn, nor nearly the entire development community doesn't see your convention in practice. Historically, in many languages, variables beginning with _ are reserved for the compiler writer and their libraries (which would also include .Net). While it is probably pretty difficult to get a name-clash in C#, historical practice still applies in the minds of most developers. – Dunk Aug 02 '12 at 18:48
  • 1
    @Dunk how is it suddenly *my* convention? I was merely passing a comment, I don't have an opinion on this matter either way :) – MattDavey Aug 02 '12 at 18:58
  • I use this convention. What's wrong about it? It just helps to distinguish (in IntelliSense) between private and public members from within the class. It also brings about a difference between parameters and class fields, so that one doesn't have to resort to using `this` all over the place (`this.url = url;` etc.) – Konrad Morawski Aug 02 '12 at 19:00
  • @Morawski - private members are already distinguished from public be being camelCased. And if you're parameters are clashing with your private members (outside of the constructor) you need a better design or names for things. – Telastyn Aug 02 '12 at 20:50
  • 2
    @MarjanVenema : not sure what you are talking about but C# is quite case sensitive and you can certainly have collisions between similarly cased members. This gets fun and exciting when you have actual case INsensitive languages like VB.NET you are working with on a library level. – Wyatt Barnett Jul 03 '13 at 18:07
  • @WyattBarnett: Oops? Wrong assumption obviously makes the rest moot. – Marjan Venema Jul 04 '13 at 06:53
  • 1
    @Telastyn member variables are not, however, distinguished from variables that are scoped to methods. I for one highly prefer member variables prefixed with underscores and I see examples of it all through code from Microsoft, so I'm not the only one. – Craig Tullis Jun 23 '16 at 02:20