2

Please bare with my I'm very new to programming.

I'm working through a tutorial of C# at the moment. And so far all variables and helper methods that I've created are capitalizedLikeThis. I've been doing this all in the Progam class.

Today I've created my first new class and within it a method, and the way the method was written WasLikeThis with the first letter also capitalized, there was no explanation to this. Is it just a common convention? And if so, is there a specific reason for it?

Thanks,

macrae2100
  • 62
  • 7
  • refer pascal and camel case in c# programming – Chandan Kumar Dec 14 '14 at 20:06
  • In C# world `WasLikeThis` (camel case) is usually the convention, in java it's more like `wasLikeThis` – AD.Net Dec 14 '14 at 20:06
  • 2
    You could find [this article on C# Coding Conventions](http://msdn.microsoft.com/en-us/library/ff926074.aspx) an interesting reading – Steve Dec 14 '14 at 20:06
  • @AD.Net thats not hungarian notation – NimChimpsky Dec 14 '14 at 20:07
  • 2
    Some programmers prefer *non-public* members to be spelled without a capitalized first letter. Like me. It is a personal preference. – Hans Passant Dec 14 '14 at 20:07
  • 1
    possible duplicate of [Naming Convention in c#](http://stackoverflow.com/questions/1618316/naming-convention-in-c-sharp) – Gabe Dec 14 '14 at 20:07
  • @NimChimpsky, you're right, brain fart on my part. – AD.Net Dec 14 '14 at 20:08
  • Methods AreLikeThis, and Properties AreLikeThis... However fields areLikeThis, and property backing fields _AreLikeThis. At least that's how we do it where I work. There is no real reason, it's just a standard. I prefer to do it this way so I know a variable is a property backing field just by it's name. I know a field is a non property backed field just by it's name, and I know whether I'm dealing with a property or a method just by it's name, because a method IsLikethis() and a Property IsLikeThis. – Ryan Mann Dec 14 '14 at 20:15
  • Personally i don't think this question is opinion based. The compiler, and even tools like resharper etc. are just enforcing some design rules that are common in the .NET framework. That is a fact. – Faris Zacina Dec 14 '14 at 20:46

2 Answers2

1

Yes there is a reason for it. It is a Pascal Case naming convention for method names that is a standard in the .NET framework.

All the types in the framework class library (FCL) follow the mentioned naming convention, and when you create some custom types or add methods to existing types you are augmenting the capabilites of the framework for your specific application needs.

Your method capitalizedLikeThis is part of the Program class, which has an API that follows the .NET naming conventions. For example it contains a ToString() instance method so you could do:

var program = new Program();
Console.WriteLine(program.ToString());

So the real question is do you want to add a method to the Program class that breaks the naming convention of the existing API? Consistency is a good thing and that is why you should follow the convention.

If you want more information on this topic you can check out the only relevant book for .NET design guidelines containing naming conventions, and many other details related to the .NET framework design decisions:

http://www.amazon.com/Framework-Design-Guidelines-Conventions-Libraries/dp/0321246756

Or read the reduced MSDN article based on the book:

http://msdn.microsoft.com/en-us/library/ms229042.aspx

Faris Zacina
  • 14,056
  • 7
  • 62
  • 75
  • 1
    No. If you read the article it says "These guidelines are excerpted from the book Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition, by Krzysztof Cwalina and Brad Abrams." – Faris Zacina Dec 14 '14 at 20:25
  • Ah, okay. I have seen that page a dozen times and I never noticed that line. – Patrick Hofman Dec 14 '14 at 20:27
  • 1
    I have a hardcopy of the book, so i know it's much larger than the article ;) – Faris Zacina Dec 14 '14 at 20:27
0

Something like SyleCop may be beneficial for you. It will enforce naming standards among other things and you can tweak the options as necessary to meet your naming standards. The built-in Visual Studio Code Analysis also has rules you can enable for this as well.

Mike Burdick
  • 838
  • 6
  • 5