微软于2015年7月21日发布了Visual Studio 2015, .NET 2015, .NET Framework 4.6, ASP.NET 4.6, Azure SDK 2.7 for .NET, C# 6.0, F# 4.0, TypeScript 1.5, Visual Studio Android 模拟器 等重量级开发产品。
由于项目升级到了.NetFramework 4.6.1,开发工具转向了VS2015,趁机尝试下C#6.0。结果网上的教程不进人意,许久都没有更新,只好自己做一下总结。
1.自动属性初始化(Auto-property initializers)
复制代码 public class Account
{
public string Name { get; set; } = "summit";
public int Age { get; set; } = 22;
public IList<int> AgeList
{
get;
set;
} = new List<int> { 10,20,30,40,50 };
}
对于只读属性也可以这样直接初始化(C#5.0不支持),也可以直接在构造函数里初始化
复制代码 public class Customer
{
public string Name { get; }
public Customer(string first, string last)
{
Name = first + " " + last;
}
}
2.字符串嵌入值(String interpolation)
在之前版本的String.Format中有多少个参数就要写多少个占位符还必须按照顺序,否则就报错.参数一多,这样搞的话确实头疼.新版本中在字符串前用$来标识后边的字符可以使用{对象}来作为占位符.看个例子.
复制代码 Console.WriteLine($"年龄:{account.Age} 生日:{account.BirthDay.ToString("yyyy-MM-dd")}");
Console.WriteLine($"年龄:{account.Age}");
Console.WriteLine($"{(account.Age<=22?"小鲜肉":"老鲜肉")}");
如果想输出{或}符号,写两个即可,如$"{{".
注意这个$符号,网上的N多教程都是没有这个东东的.是直接"{ account.Age }",这种方式在新版本里已经被抛弃了.










