命名约定
如果对于单行来说过长,则可以在点 (.) 后中断限定名称,如下面的示例所示。
布局约定
智能缩进、4 字符缩进、制表符保存为空格.每行只写一条语句。 每行只写一个声明。如果连续行未自动缩进,请将它们缩进一个制表符位(四个空格)。在方法定义与属性定义之间添加至少一个空白行。使用括号突出表达式中的子句,如下面的代码所示
if ((val1 > val2) && (val1 > val3))
{
// Take appropriate action.
}
语言准则
String 数据类型
使用字符串内插来连接短字符串,如下面的代码所示。
string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";
若要在循环中追加字符串,尤其是在使用大量文本时,请使用 StringBuilder 对象。
var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
var manyPhrases = new StringBuilder();
for (var i = 0; i < 10000; i++)
{
manyPhrases.Append(phrase);
}
//Console.WriteLine("tra" + manyPhrases);
隐式类型的局部变量
当变量类型明显来自赋值的右侧时,或者当精度类型不重要时,请对本地变量进行隐式类型化。
// When the type of a variable is clear from the context, use var
// in the declaration.
var var1 = "This is clearly a string.";
var var2 = 27;
var var3 = Convert.ToInt32(Console.ReadLine());
当类型并非明显来自赋值的右侧时,请勿使用 var。 int var4 = ExampleClass.ResultSoFar();使用隐式类型化(var)来确定 for 和 foreach 循环中循环变量的类型。
无符号数据类型
通常,使用 int 而非无符号类型。 int 的使用在整个 C# 中都很常见,并且当你使用 int 时,更易于与其他库交互。
数组
当在声明行上初始化数组时,请使用简洁的语法。
// Preferred syntax. Note that you cannot use var here instead of string[].
string[] vowels1 = { "a", "e", "i", "o", "u" };
// If you use explicit instantiation, you can use var.
var vowels2 = new string[] { "a", "e", "i", "o", "u" };
// If you specify an array size, you must initialize the elements one at a time.
var vowels3 = new string[5];
vowels3[0] = "a";
vowels3[1] = "e";
// And so on.
委托
创建及使用委托类型的步骤。
//1,定义委托 in class Program, define the delegate type and a method that
// has a matching signature.
// Define the type.
public delegate void Del(string message);
// 2,定义方法Define a method that has a matching signature.
public static void DelMethod(string str)
{
Console.WriteLine($"DelMethod argument: {str}");
}
// 3,将方法添加到委托,一个委托可以添加多个方法(+=)。
//In the Main method, create an instance of Del.
// Preferred: Create an instance of Del by using condensed syntax(简介语法).
Del exampleDel2 = DelMethod;
// The following declaration uses the full syntax(常规语法).
Del exampleDel1 = new Del(DelMethod);
New 运算符
隐式类型化时,请使用对象实例化的简洁形式,如下面的声明所示。
//隐式类型化
var instance1 = new ExampleClass();
//常规模式
ExampleClass instance2 = new ExampleClass();
使用对象初始值设定项来简化对象创建。
// Object initializer.
var instance3 = new ExampleClass { Name = "Desktop", ID = 37414,
Location = "Redmond", Age = 2.3 };
// Default constructor and assignment statements.
var instance4 = new ExampleClass();
instance4.Name = "Desktop";
instance4.ID = 37414;
instance4.Location = "Redmond";
instance4.Age = 2.3;
事件处理
如果你正定义一个稍后不需要删除的事件处理程序,请使用 lambda 表达式。
//lambda方式
public Form2()
{
// You can use a lambda expression to define an event handler.
this.Click += (s, e) =>
{
MessageBox.Show(
((MouseEventArgs)e).Location.ToString());
};
}
//传统方式
// Using a lambda expression shortens the following traditional definition.
public Form1()
{
this.Click += new EventHandler(Form1_Click);
}
void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show(((MouseEventArgs)e).Location.ToString());
}