首页 技术 正文
技术 2022年11月15日
0 收藏 494 点赞 4,104 浏览 2790 个字

由于项目升级到了.NetFramework 4.6.1,开发工具转向了vs2015,趁机尝试下C#6.0.结果在网上搜的一些教程总结的不是太完整,有的代码随着vs正式版的发布也有所修改.那些个教程也没更新.
所以把自己学习到的记录一下.

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 \}”,这种方式在新版本里已经被抛弃了.

3.导入静态类(Using Static)

像之前使用Math这个静态类的时候要先导入System命名空间后才能使用它.现在可以直接导入这个静态,然后代码中直接使用其函数.

using static System.Math;//注意这里不是命名空间哦Console.WriteLine($"之前的使用方式: {Math.Pow(4, 2)}");
Console.WriteLine($"导入后可直接使用方法: {Pow(4,2)}");

注意这里是using static …
如果某个命名空间下有n个方法,用这种方式直接引入单个静态类而不用引用所有方法还是挺方便的.

4.空值运算符(Null-conditional operators)

之前写过无数个这样的判断代码

if (*** != null)
{
//不为null的操作
}
return null;

现在使用可以简化这样方式.

var age = account.AgeList?[0].ToString();Console.WriteLine("{0}", (person.list?.Count ?? 0));

如果account.AgeList为空则整个表达式返回空,否则后边的表达式的值.

5.对象初始化器(Index Initializers)

这种方式可以给字典或其他对象通过索引赋值初始化.

IDictionary<int, string> dict = new Dictionary<int, string>() {
[1]="first",
[2]="second"
};
foreach(var dic in dict)
{
Console.WriteLine($"key: {dic.Key} value:{dic.Value}");
}output:
key: 1 value:first
key: 2 value:second

6.异常过滤器(Exception filters)

private static bool Log(Exception e)
{
Console.WriteLine("log");
return true;
}
static void TestExceptionFilter()
{
try
{
Int32.Parse("s");
}
catch (Exception e) when (Log(e))
{
Console.WriteLine("catch");
return;
}
}当when()里面返回的值不为true,将持续抛出异常,不会执行catch里面的方法.

7.nameof表达式 (nameof expressions)

在对方法参数进行检查时经常这样写:
private static void Add(Account account)
{
if (account == null)
throw new ArgumentNullException("account");
}如果某天参数的名字被修改了,下面的字符串很容易漏掉忘记修改.
使用nameof表达式后,编译的时候编译器将检查到有修改后自动导航与重构(-_-! 不知道翻译的对不对)
private static void Add(Account account)
{
if (account == null)
throw new ArgumentNullException(nameof(account));
}

8.在cath和finally语句块里使用await(Await in catch and finally blocks)

c#5.0里是不支持这么写.Resource res = null;
try
{
res = await Resource.OpenAsync(…); // You could do this.

}
catch(ResourceException e)
{
await Resource.LogAsync(res, e); // Now you can do this …
}
finally
{
if (res != null) await res.CloseAsync(); // … and this.
}

9.在属性里使用Lambda表达式(Expression bodies on property-like function members)

public string Name =>string.Format("姓名: {0}", "summit");public void Print() => Console.WriteLine(Name);

10.在方法成员上使用Lambda表达式

static int LambdaFunc(int x, int y) => x*y;public void Print() => Console.WriteLine(First + " " + Last);

关于C#6.0新增加的语言特性目前为止也就这么多.没有什么新功能,更多的是语法糖的改进.开发更舒服更快速. 
最后附上github上关于c#6.0的相关介绍链接:点我点我

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905