Pascal casing or Camel Casing for C# code?

75,244

Solution 1

I use what the Framework uses, as it's the de-facto best practice. However, so long as the code in your company is consistently using their style, then you're much better off getting used to it. If every developer has their own standard, then there's no standard at all.

Solution 2

A link to the official design guidelines might help. Specifically, read the section on Capitalization styles.

In the grand scheme of things, Pascal vs Camel doesn't matter that much and you're not likely to convince anyone to go back over an existing code base just to change the case of names. What's really important is that you want to be consistent within a given code base.

I'm just happy as long as you're not using Hungarian.

Solution 3

You should have a look at Microsoft's new tool, StyleCop for checking C# source code. Also keep an eye on FxCop for checking compiled .Net assemblies. FxCop focuses more on the details of what the code does, not the layout, but it does have some naming rules related to publicly visible names.

StyleCop defines a coding standard, which is now being promoted by Microsoft as an industry standard. It checks C# source code against the standard. StyleCop adheres to your PascalCase style.

Getting people onto StyleCop (or any other standard for that matter) can be hard, it's quite a hurdle, and StyleCop is quite exhaustive. But code should be to a uniform standard - and a personal standard is better than none, company standard is better than a personal one, and an industry standard is best of all.

It's a lot easier to convince people when a a project starts - team is being formed and there is no existing code to convert. And you can put tools (FxCop, StyleCop) in place to break the build if the code does not meet standards.

You should use the standard for the language and framework - SQL code should use SQL standards, and C# code should use C# standards.

Solution 4

For public interfaces you should stick with the MS .NET framework design guidelines: "Capitalization Conventions".

For non-exposed members then whatever you and your colleagues can agree on.

Solution 5

I (and my team) prefer to reserve initial capitals for class names.

Why? Java standards propagating, I think.

Share:
75,244
Gustavo Rubio
Author by

Gustavo Rubio

I'm a software developer and entrepeneur based in Tijuana Mexico. Working with a mix of .Net technologies and OpenSource (Linux, PHP, Python, Ruby etc).

Updated on January 14, 2020

Comments

  • Gustavo Rubio
    Gustavo Rubio over 4 years

    I've been arguing with my coworkers about Pascal casing (upper camel case) vs. lower CamelCasing. They are used to lower camel casing for everything from table names in SQL databases to property naming in C# code but I like Pascal casing better, lower camel casing for variables and Pascal casing for properties:

    string firstName;
    public string FirstName {
    ...
    }
    

    But they are used to this:

    string _firstname;
    public string firstName {
    ...
    }
    

    I try to keep up with their "standard" so the code looks the same but I just don't like it.

    I've seen that at least the .NET framework uses this convention and that is how I try to keep my code, e.g.:

    System.Console.WriteLine("string")
    

    What do you use/prefer and why? I'm sorry if somebody else asked this question but I searched and did not find anything.

    Update: I've given a method example and not a property but it's the same. As I stated in the first paragraph my colleagues use the Pascal convention for everything (variables, methods, table names, etc.)