以下各部分描述了 C# 团队准备代码示例时遵循的做法。
String 数据类型
隐式类型化局部变量
-
如果赋值语句右侧的变量类型十分明显,或在精确的类型并不重要时,使用 隐式类型 的局部变量。
// 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。
// When the type of a variable is not clear from the context, use an
// explicit type.
int var4 = ExampleClass.ResultSoFar();
-
不要依赖变量名来指定变量的类型。 这可能是不正确的。
// Naming the following variable inputInt is misleading.
// It is a string.
var inputInt = Console.ReadLine();
Console.WriteLine(inputInt);
-
避免使用 var 取代 dynamic。
-
使用隐式类型在 for 和 foreach 循环中确定循环变量的类型。
下面的示例在 for 语句中使用了隐式类型。
var syllable = "ha";
var laugh = "";
for (var i = 0; i < 10; i++)
{
laugh += syllable;
Console.WriteLine(laugh);
}
下面的示例在 foreach 语句中使用了隐式类型。
foreach (var ch in laugh)
{
if (ch == 'h')
Console.Write("H");
else
Console.Write(ch);
}
Console.WriteLine();
无符号数据类型
数组
-
在您初始化声明行上的数组时,请使用简洁的语法。
// 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.
委托
-
请使用简洁的语法来创建委托类型的实例。
// First, in class Program, define the delegate type and a method that
// has a matching signature.
// Define the type.
public delegate void Del(string message);
// Define a method that has a matching signature.
public static void DelMethod(string str)
{
Console.WriteLine("DelMethod argument: {0}", str);
}
// 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);
异常处理中的 try-catch 和 using 语句
&& 和 || 运算符
New 运算符
事件处理
静态成员
LINQ 查询
-
对于查询变量使用有意义的名称。 下面的示例使用 seattleCustomers 代表地处西雅图的客户。
var seattleCustomers = from cust in customers
where cust.City == "Seattle"
select cust.Name;
-
使用别名以确保通过 Pascal 大小写格式正确设置匿名类型的属性名称的大小写。
var localDistributors =
from customer in customers
join distributor in distributors on customer.City equals distributor.City
select new { Customer = customer, Distributor = distributor };
-
在结果中的属性名称不明确时重命名属性。 例如,如果查询返回一个客户名称和一个分销商 ID,不要在结果中将它们保留为 Name 和 ID,而是将它们重命名,以阐明 Name 是客户的名称,ID 是分销商的 ID。
var localDistributors2 =
from cust in customers
join dist in distributors on cust.City equals dist.City
select new { CustomerName = cust.Name, DistributorID = dist.ID };
-
使用隐式类型来声明查询变量和范围变量。
var seattleCustomers = from cust in customers
where cust.City == "Seattle"
select cust.Name;
-
将查询子句对齐在 from 子句下方,如前面的示例所示。
-
在其他查询子句前面使用 where 子句,以确保对一组经过简化和筛选的数据执行后面的查询子句。
var seattleCustomers2 = from cust in customers
where cust.City == "Seattle"
orderby cust.Name
select cust;
-
使用多个 from 子句而不是一个 join 子句来访问内部集合。 例如,在 Student 对象集合中,每个对象可能都包含一个考试成绩集合。 在执行下面的查询时,它将返回每一个超过 90 的分数,以及获得该分数的学生的姓氏。
// Use a compound from to access the inner sequence within each element.
var scoreQuery = from student in students
from score in student.Scores
where score > 90
select new { Last = student.LastName, score };
|