命名惯例和规范
注记: Pascal 大小写形式-所有单词第一个字母大写,其他字母小写。
Camel大小写形式-除了第一个单词,所有单词第一个字母大写,其他字母小写。
public class HelloWorld
{ ...
}
|
public class HelloWorld
{
void SayHello(string name)
{
...
}
}< |
public class HelloWorld
{
int totalCount = 0;
void SayHello(string name)
{
string fullMessage = "Hello " + name;
...
}
}
|
以前,多数程序员喜欢它-把数据类型作为变量名的前缀而m_作为成员变量的前缀。例如:
string m_sName; int nAge;然而,这种方式在.NET编码规范中是不推荐的。所有变量都用camel 大小写形式,而不是用数据类型和m_来作前缀。
- 别用缩写。用name, address, salary等代替 nam, addr, sal
- 别使用单个字母的变量象i, n, x 等. 使用 index, temp等
用于循环迭代的变量例外:
for ( int i = 0; i < count; i++ )
{
...
}
如果变量只用于迭代计数,没有在循环的其他地方出现,许多人还是喜欢用单个字母的变量(i) ,而不是另外取名。 - 变量名中不使用下划线 (_) 。
- 命名空间需按照标准的模式命名
. . .
例如,对于类HelloWorld, 相应的文件名应为 helloworld.cs (或, helloworld.vb)
缩进和间隔
bool SayHello (string name)
{
string fullMessage = "Hello " + name;
DateTime currentTime = DateTime.Now;
string message = fullMessage + ", the time is: " + currentTime.ToShortTimeString();
MessageBox.Show ( message );
if ( ... )
{
// Do something
// ...
return false;
}
return true;
} |
bool SayHello ( string name )
{
string fullMessage = "Hello " + name;
DateTime currentTime = DateTime.Now;
the time is: " + currentTime.ToShortTimeString(); |
好:
if ( ... )
{ // Do something
}
不好: if ( ... ) {
// Do something
}
好: if ( showResult == true )
{
for ( int i = 0; i < 10; i++ )
{
//
}
}
不好: if(showResult==true)
{
for(int i= 0;i<10;i++)
{
//
}
}
|
内容导航


























