本文的内容包括引入C#6.0中的新的语言特性有哪些. 还有已经被引入的代码名称为 “Roslyn”新https://roslyn.codeplex.com/.
C# 6.0 中的新特性
我们可以对这些新特性一个一个的进行讨论,而首先要列出 C# 6.0 中这些特性的一个清单
-
自动的属性初始化器 Auto Property Initializer
-
主构造器 Primary Consturctor
-
字典初始化器 Dictionary Initializer
-
声明表达式 Declaration Expression
-
静态的Using Static Using
-
catch 块中的 await
-
异常 Exception Filter
-
用于检查NULL值的条件访问操作符
1. 自动的属性初始化器Auto Property initialzier
之前的方式
初始化一个自动属性Auto Property的唯一方式,就是去实现一个明确的构造器,在里面对属性值进行设置.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class AutoPropertyBeforeCsharp6
{ private string _postTitle = string.Empty;
public AutoPropertyBeforeCsharp6()
{
//assign initial values
PostID = 1;
PostName = "Post 1";
}
public <span id="8_nwp" style="width: auto; height: auto; float: none;"><a id="8_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=e73d8e665ea45c13&k=long&k0=long&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=135ca45e668e3de7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6014%2Ehtml&urlid=0" target="_blank" mpid="8" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">long</span></a></span> PostID { get; set; }
public string PostName { get; set; }
public string PostTitle
{
get { return _postTitle; }
protected set
{
_postTitle = value;
}
}
} |
有了这个特性之后的方式
使用 C# 6 自动实现的带有初始值的属性可以不用编写构造器就能被初始化. 我们可以用下面的代码简化上面的示例:
|
1
2
3
4
5
6
7
8
|
public class AutoPropertyInCsharp6
{ public long PostID { get; } = 1;
public string PostName { get; } = "Post 1";
public string PostTitle { get; protected set; } = string.Empty;
} |
2. 主构造器
我们使用构造器主要是来初始化里面的值.(接受参数值并将这些参数值赋值给实体属性).
之前的方式
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class PrimaryConstructorsBeforeCSharp6
{ public PrimaryConstructorsBeforeCSharp6(long postId, string postName, string postTitle)
{
PostID = postId;
PostName = postName;
PostTitle = postTitle;
}
public <span id="7_nwp" style="width: auto; height: auto; float: none;"><a id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=e73d8e665ea45c13&k=long&k0=long&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=135ca45e668e3de7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6014%2Ehtml&urlid=0" target="_blank" mpid="7" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">long</span></a></span> PostID { get; set; }
public string PostName { get; set; }
public string PostTitle { get; set; }
} |
有了这个特性之后的方式
|
1
2
3
4
5
6
|
public class PrimaryConstructorsInCSharp6(<span id="6_nwp" style="width: auto; height: auto; float: none;"><a id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=e73d8e665ea45c13&k=long&k0=long&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=135ca45e668e3de7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6014%2Ehtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">long</span></a></span> 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 后面添加额外的设置
<LangVersion>experimental</LangVersion>
‘主构造器’只在‘实验’性质的语言版本中可用
3. 字典初始化器
之前的方式
编写一个字典初始化器的老办法如下
|
1
2
3
4
5
6
7
8
|
public class DictionaryInitializerBeforeCSharp6
{ public Dictionary<string, string> _users = new Dictionary<string, string>()
{
{"users", "Venkat Baggu Blog" },
{"Features", "Whats new in C# 6" }
};
} |
有了这个特性之后的方式
我们可以像数组里使用方括号的方式那样定义一个字典初始化器
|
1
2
3
4
5
6
7
8
|
public class DictionaryInitializerInCSharp6
{ public Dictionary<string, string> _users { get; } = new Dictionary<string, string>()
{
["users"] = "Venkat Baggu Blog",
["Features"] = "Whats new in C# 6"
};
} |
4. 声明表达式
之前的方式
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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(<span id="5_nwp" style="width: auto; height: auto; float: none;"><a id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=e73d8e665ea45c13&k=long&k0=long&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=135ca45e668e3de7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6014%2Ehtml&urlid=0" target="_blank" mpid="5" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">long</span></a></span> userId)
{
////Example 2
var user = _userRepository.Users.FindById(x => x.UserID == userId);
if (user!=null)
{
// work with address ...
return user.City;
}
}
} |
有了这个特性之后的方式
在 C# 6 中你可以在表达式的中间声明一个本地变量. 使用声明表达式我们还可以在if表达式和各种循环表达式中声明变量
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class DeclarationExpressionsInCShapr6()
{ public static int CheckUserExist(string userId)
{
if (!int.TryParse(userId, out var id))
{
return id;
}
return 0;
}
public static string GetUserRole(<span id="3_nwp" style="width: auto; height: auto; float: none;"><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=e73d8e665ea45c13&k=long&k0=long&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=135ca45e668e3de7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6014%2Ehtml&urlid=0" target="_blank" mpid="3" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">long</span></a></span> userId)
{
////Example 2
if ((var user = _userRepository.Users.FindById(x => x.UserID == userId) != null)
{
// work with address ...
return user.City;
}
}
} |
5. 静态的 Using
之前的方式
对于你的静态成员而言,没必要为了调用一个方法而去弄一个对象实例. 你会使用下面的语法
|
1
2
3
4
5
6
7
|
TypeName.MethodNamepublic class StaticUsingBeforeCSharp6
{ public void TestMethod()
{
Console.WriteLine("Static Using Before C# 6");
}
} |
之后的方式
在 C# 6 中,你不用类名就能使用 静态成员 . 你可以在命名空间中引入静态类.
如果你看了下面这个实例,就会看到我们将静态的Console类移动到了命名空间中
|
1
2
3
4
5
6
7
8
9
10
11
|
using System.Console;
namespace newfeatureincsharp6
{ public class StaticUsingInCSharp6
{
public void TestMethod()
{
WriteLine("Static Using Before C# 6");
}
}
} |
6. catch块里面的await
C# 6 之前catch和finally块中是不能用 await 关键词的. 在 C# 6 中,我们终于可以再这两个地方使用await了.
|
1
2
3
4
5
6
7
8
|
try{ //Do something
}catch (Exception)
{ await Logger.Error("exception logging")
} |
7. 异常过滤器
异常过滤器可以让你在catch块执行之前先进行一个 if 条件判断.
看看这个发生了一个异常的示例,现在我们想要先判断里面的Exception是否为null,然后再执行catch块
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
//示例 1try{ //Some code
}catch (Exception ex) if (ex.InnerException == null)
{ //Do work
}//Before C# 6 we write the above code as follows//示例 1try{ //Some code
}catch (Exception ex)
{ if(ex.InnerException != null)
{
//Do work;
}
} |
8. 用于检查NULL值的条件访问操作符?.
看看这个实例,我们基于UserID是否不为null这个条件判断来提取一个UserRanking.
之前的方式
|
1
2
3
4
5
6
7
8
9
|
var userRank = "No Rank";
if(UserID != null)
{ userRank = Rank;
}//orvar userRank = UserID != null ? Rank : "No Rank"
|
有了这个特性之后方式
|
1
|
var userRank = UserID?.Rank ?? "No Rank";
|
C# 6.0中的新特性 第一次出现在 Venkat Baggu 博客 上.
原文地址:http://www.codeproject.com/Articles/874205/New-features-in-Csharp