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.
9
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.
2
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?
1
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.
12