Pascal casing or Camel Casing for C# code?

2019-01-14 10:43发布

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.)

14条回答
唯我独甜
2楼-- · 2019-01-14 11:07

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-14 11:08

Pascal casing should be used for Properties. As far as varible names go, some people use _ and some poeple use m_ and some people just use plain old camel casing. I think that as long as you ae consistant here, it shouldn't matter.

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-14 11:09

Actually, there's no "standard" convention on this. There's a Microsoft edited guideline somewhere, and as with with any other naming convention guideline, surely there's another one refuting it, but here's what I've come to understand as "standard C# casing convention".

  1. PerWordCaps in type names (classes, enums), constants and properties.
  2. camelCase for really long local variables and protected/private variables
  3. No ALL_CAPS ever (well, only in compiler defines, but not in your code)
  4. It seems some of the system classes use underscored names (_name) for private variables, but I guess that comes from the original writer's background as most of them came straight from C++. Also, notice that VB.NET isn't case sensitive, so you wouldn't be able to access the protected variables if you extended the class.

Actually, FxCop will enforce a few of those rules, but (AFAIK) it ignores whatever spelling you use for local variables.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-14 11:15

Whichever you prefer is what matters, obviously adhering to the team's standard primarily. In private you code however you want, it doesn't affect the finished product whether you named your variable someVariable or SomeVariable.

查看更多
Rolldiameter
6楼-- · 2019-01-14 11:16

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.

查看更多
冷血范
7楼-- · 2019-01-14 11:17

From .NET Framework Developer's Guide Capitalization Conventions, Case-Sensitivity:

The capitalization guidelines exist solely to make identifiers easier to read and recognize. Casing cannot be used as a means of avoiding name collisions between library elements.

Do not assume that all programming languages are case-sensitive. They are not. Names cannot differ by case alone.

查看更多
登录 后发表回答