I started working with C# recently and I noticed that the convention seems to be that the variables start with a capital letter along with the methods.
我最近开始使用C#,我注意到约定似乎是变量以大写字母和方法开头。
Is this the only language that does this and why? For instance:
这是唯一可以做到这一点的语言吗?为什么?例如:
Page Page = new Page();
Page.Action();
In other languages, you'd see instead:
在其他语言中,您会看到:
Page page = new Page();
page.action();
There are other examples of this that are confusing since I've worked a lot with UML, Ruby, C++ and Java.
由于我在UML,Ruby,C ++和Java方面做了大量工作,因此还有其他一些令人困惑的例子。
My question is, why does C# do it this way when other languages do not?
我的问题是,当其他语言没有时,为什么C#会这样做呢?
Edit
Other Stack Overflow users are noting that C# does not follow this convention, this was just a mistake on my part.
其他Stack Overflow用户注意到C#不遵循这个惯例,这只是我的错误。
8 个解决方案
#1
Well actually, no: the convention in C# is for camelCased variable (and field) names, and PascalCase methods:
实际上,没有:C#中的约定是针对camelCased变量(和字段)名称,以及PascalCase方法:
Page page = new Page();
page.Action();
#2
No, this is fairly non-standard C# code. The .Net Framework Design guidelines in section 2.6 recomend the use of camel casing for local variable names.
不,这是相当非标准的C#代码。 2.6节中的.Net框架设计指南建议使用驼峰套管作为局部变量名。
#3
Public members use PascalCase
, private members use camelCase
.
公共成员使用PascalCase,私有成员使用camelCase。
I think that this makes it clearer which methods support a class versus which methods define a class.
我认为这使得哪个方法支持类与哪些方法定义类更清楚。
public class Foo
{
private Bar bar;
public Bar Bar { get; set; }
public void DoFoo()
{
makeFoo();
}
private void makeFoo()
{
}
}
#4
Every set of C# conventions I've written or used would specify camel case for variables, so:
我编写或使用的每组C#约定都会为变量指定驼峰大小写,因此:
Page page = new Page();
MyClass myClass = new MyClass();
Not sure where you've seen Pascal case used, but it's certainly not something inherent to, or standard for, C#.
不确定你在哪里看过Pascal案例,但它肯定不是C#固有或标准的东西。
#5
Variables in C# typically don't start with an uppercase letter (though that's obviously up to the developer's own implementation). What you're probably confused about is the concept of a Property. Properties in C# are used syntactically like variables (in that they can be retrieved or assigned, rather than just executed like a function), but can encapsulate business logic on both operations.
C#中的变量通常不以大写字母开头(尽管这显然取决于开发人员自己的实现)。您可能感到困惑的是财产的概念。 C#中的属性在语法上与变量一样使用(因为它们可以被检索或分配,而不是像函数一样执行),但可以在两个操作上封装业务逻辑。
To answer a broader part of your question, though, both properties and methods/functions do typically start with an uppercase letter according to the Microsoft guidelines.
但是,为了回答问题的更广泛部分,根据Microsoft指南,属性和方法/函数通常都以大写字母开头。
#6
PascalCase was a convention at Microsoft long before .NET. (In the Win32 API etc.)
早在.NET之前,PascalCase就是微软的一项会议。 (在Win32 API等中)
A convention also makes sense to use within a single environment. .NET being a comprehensive environment on its own, and Microsoft-the-company another, there's really no point to adopting someone else's.
在单个环境中使用约定也是有意义的。 .NET本身就是一个全面的环境,而另一个是微软公司,那么采用其他人就没有意义了。
Also, I strongly doubt UML has a naming convention or that even the idea of UML having a naming convention makes sense. UML models your software, and should follow the convention of that software.
此外,我强烈怀疑UML有一个命名约定,或者即使是具有命名约定的UML的想法也是有道理的。 UML为您的软件建模,并且应该遵循该软件的惯例。
#7
You can find out a lot about what conventions should be adopted by using the two following tools.
通过使用以下两个工具,您可以了解应采用哪些约定。
FxCop: http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx
#8
Public = PascalCase, I only tend to use camelCase for private fields, and parameter arguments.
Public = PascalCase,我只倾向于将camelCase用于私有字段和参数参数。
Read this coding standard though easily searchable on google, not a bad place to start.
阅读此编码标准虽然可以在谷歌上轻松搜索,但不是一个糟糕的起点。
#1
Well actually, no: the convention in C# is for camelCased variable (and field) names, and PascalCase methods:
实际上,没有:C#中的约定是针对camelCased变量(和字段)名称,以及PascalCase方法:
Page page = new Page();
page.Action();
#2
No, this is fairly non-standard C# code. The .Net Framework Design guidelines in section 2.6 recomend the use of camel casing for local variable names.
不,这是相当非标准的C#代码。 2.6节中的.Net框架设计指南建议使用驼峰套管作为局部变量名。
#3
Public members use PascalCase
, private members use camelCase
.
公共成员使用PascalCase,私有成员使用camelCase。
I think that this makes it clearer which methods support a class versus which methods define a class.
我认为这使得哪个方法支持类与哪些方法定义类更清楚。
public class Foo
{
private Bar bar;
public Bar Bar { get; set; }
public void DoFoo()
{
makeFoo();
}
private void makeFoo()
{
}
}
#4
Every set of C# conventions I've written or used would specify camel case for variables, so:
我编写或使用的每组C#约定都会为变量指定驼峰大小写,因此:
Page page = new Page();
MyClass myClass = new MyClass();
Not sure where you've seen Pascal case used, but it's certainly not something inherent to, or standard for, C#.
不确定你在哪里看过Pascal案例,但它肯定不是C#固有或标准的东西。
#5
Variables in C# typically don't start with an uppercase letter (though that's obviously up to the developer's own implementation). What you're probably confused about is the concept of a Property. Properties in C# are used syntactically like variables (in that they can be retrieved or assigned, rather than just executed like a function), but can encapsulate business logic on both operations.
C#中的变量通常不以大写字母开头(尽管这显然取决于开发人员自己的实现)。您可能感到困惑的是财产的概念。 C#中的属性在语法上与变量一样使用(因为它们可以被检索或分配,而不是像函数一样执行),但可以在两个操作上封装业务逻辑。
To answer a broader part of your question, though, both properties and methods/functions do typically start with an uppercase letter according to the Microsoft guidelines.
但是,为了回答问题的更广泛部分,根据Microsoft指南,属性和方法/函数通常都以大写字母开头。
#6
PascalCase was a convention at Microsoft long before .NET. (In the Win32 API etc.)
早在.NET之前,PascalCase就是微软的一项会议。 (在Win32 API等中)
A convention also makes sense to use within a single environment. .NET being a comprehensive environment on its own, and Microsoft-the-company another, there's really no point to adopting someone else's.
在单个环境中使用约定也是有意义的。 .NET本身就是一个全面的环境,而另一个是微软公司,那么采用其他人就没有意义了。
Also, I strongly doubt UML has a naming convention or that even the idea of UML having a naming convention makes sense. UML models your software, and should follow the convention of that software.
此外,我强烈怀疑UML有一个命名约定,或者即使是具有命名约定的UML的想法也是有道理的。 UML为您的软件建模,并且应该遵循该软件的惯例。
#7
You can find out a lot about what conventions should be adopted by using the two following tools.
通过使用以下两个工具,您可以了解应采用哪些约定。
FxCop: http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx
#8
Public = PascalCase, I only tend to use camelCase for private fields, and parameter arguments.
Public = PascalCase,我只倾向于将camelCase用于私有字段和参数参数。
Read this coding standard though easily searchable on google, not a bad place to start.
阅读此编码标准虽然可以在谷歌上轻松搜索,但不是一个糟糕的起点。