Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 is capitalises the first letter 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.

share|improve this question
12  
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 2 '12 at 5:00
    
Use JavaScript instead.. for Unity3D :) –  Lipis Aug 7 '12 at 0:54
    
@Lipis Why for - other than personal taste? ;-) –  AedonEtLIRA Aug 7 '12 at 14:58
    
@AedonEtLIRA totally personal.. never mind :) Enjoy Unity no matter what language.. you'll end up using.. –  Lipis Aug 7 '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. –  AedonEtLIRA Aug 7 '12 at 22:29
show 1 more comment

3 Answers

up vote 21 down vote accepted

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.

share|improve this answer
    
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 8 '13 at 9:42
add comment

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?

share|improve this answer
1  
A +1 for this because it gives what may be the actual reason why this particular arbitrary choice was made for C#. –  Jimmy Shelter Jul 3 '13 at 20:11
add comment

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.

share|improve this answer
    
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 2 '12 at 6:10
1  
The underscore should not be used in C# private fields (see blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx, e.g), but it's controversial and often found. –  Jens Aug 2 '12 at 7: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 2 '12 at 10:11
3  
@Telastyn that statement is pretty shocking. Most of the .NET class library uses that convention. –  MattDavey Aug 2 '12 at 14:31
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 2 '12 at 18:58
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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