本文的内容包括引入C#6.0中的新的语言特性有哪些. 还有已经被引入的代码名称为 “Roslyn”新编译器. 编译器是开放源码的,并且可以从 codeplex 网站的这个地址下载到源代码:?https://roslyn.codeplex.com/.
我们可以对这些新特性一个一个的进行讨论,而首先要列出 C# 6.0 中这些特性的一个清单
初始化一个自动属性Auto Property的唯一方式,就是去实现一个明确的构造器,在里面对属性值进行设置.
public class AutoPropertyBeforeCsharp6
{
? ? private string _postTitle = string.Empty;
? ? public AutoPropertyBeforeCsharp6()
? ? {
? ? ? ? //assign initial values
? ? ? ? PostID = 1;
? ? ? ? PostName = "Post 1";
? ? }
?
? ? public long PostID { get; set; }
?
? ? public string PostName { get; set; }
?
? ? public string PostTitle
? ? {
? ? ? ? get { return _postTitle; }
? ? ? ? protected set
? ? ? ? {
? ? ? ? ? ? _postTitle = value;
? ? ? ? }
? ? }
}
有了这个特性之后的方式
使用 C# 6 自动实现的带有初始值的属性可以不用编写构造器就能被初始化. 我们可以用下面的代码简化上面的示例:
public class AutoPropertyInCsharp6
{
? ? public long PostID { get;? } = 1;
?
? ? public string PostName { get; } = "Post 1";
?
? ? public string PostTitle { get; protected set; } = string.Empty;
}
2. 主构造器
我们使用构造器主要是来初始化里面的值.(接受参数值并将这些参数值赋值给实体属性).
之前的方式
?
public class PrimaryConstructorsBeforeCSharp6
{
? ? public PrimaryConstructorsBeforeCSharp6(long postId, string postName, string postTitle)
? ? {
? ? ? ? PostID = postId;
? ? ? ? PostName = postName;
? ? ? ? PostTitle = postTitle;
? ? }
?
? ? public long PostID { get; set; }
? ? public string PostName { get; set; }
? ? public string PostTitle { get; set; }
}
有了这个特性之后的方式
public class PrimaryConstructorsInCSharp6(long postId, string postName, string postTitle)
{? ? ? ?
? ? public long PostID { get;? } = postId;
? ? public string PostName { get; } = postName;
? ? public string PostTitle { get;? } = postTitle;
}
?
在 C# 6 中, 主构造器为我们提供了使用参数定义构造器的一个简短语法. 每个类只可以有一个主构造器.
如果你观察上面的示例,会发现我们将参数初始化移动到了类名的旁边.
你可能会得到下面这样的错误“Feature ‘primary constructor’ is only available in ‘experimental’ language version.”(主构造器特性只在实验性质的语言版本中可用), 为了解决这个问题,我们需要编辑 SolutionName.csproj 文件,来规避这个错误 . 你所要做的就是在 WarningTag 后面添加额外的设置
experimental
Feature 'primary constructor' is only available in 'experimental' language version

‘主构造器’只在‘实验’性质的语言版本中可用
3. 字典初始化器
之前的方式
编写一个字典初始化器的老办法如下
public class DictionaryInitializerBeforeCSharp6
{
? ? public Dictionary _users = new Dictionary()
? ? {
? ? ? ? {"users", "Venkat Baggu Blog" },
? ? ? ? {"Features", "Whats new in C# 6" }
? ? };
}
有了这个特性之后的方式
我们可以像数组里使用方括号的方式那样定义一个字典初始化器
public class DictionaryInitializerInCSharp6
{
? ? public Dictionary _users { get; } = new Dictionary()
? ? {
? ? ? ? ["users"]? = "Venkat Baggu Blog",
? ? ? ? ["Features"] =? "Whats new in C# 6"
? ? };
}
?
4. 声明表达式
之前的方式
public class DeclarationExpressionsBeforeCShapr6()
{
? ? public static int CheckUserExist(string userId)
? ? {
? ? ? ? //Example 1
? ? ? ? int id;
? ? ? ? if (!int.TryParse(userId, out id))
? ? ? ? {
? ? ? ? ? ? return id;
? ? ? ? }
? ? ? ? return id;
? ? }
?
? ? public static string GetUserRole(long userId)
? ? {
? ? ? ? ////Example 2
? ? ? ? var user = _userRepository.Users.FindById(x => x.UserID == userId);
? ? ? ? if (user!=null)
? ? ? ? {
? ? ? ? ? ? // work with address ...
?
? ? ? ? ? ? return user.City;
? ? ? ? }
? ? }
}
有了这个特性之后的方式
在 C# 6 中你可以在表达式的中间声明一个本地变量. 使用声明表达式我们还可以在if表达式和各种循环表达式中声明变量
public class DeclarationExpressionsInCShapr6()
{
? ? public static int CheckUserExist(string userId)
? ? {
? ? ? ? if (!int.TryParse(userId, out var id))
? ? ? ? {
? ? ? ? ? ? return id;
? ? ? ? }
? ? ? ? return 0;
? ? }
?
? ? public static string GetUserRole(long userId)
? ? {
? ? ? ? ////Example 2
? ? ? ? if ((var user = _userRepository.Users.FindById(x => x.UserID == userId) != null)
? ? ? ? {
? ? ? ? ? ? // work with address ...
?
? ? ? ? ? ? return user.City;
? ? ? ? }
? ? }
}
5. 静态的 Using
之前的方式
对于你的静态成员而言,没必要为了调用一个方法而去弄一个对象实例. 你会使用下面