目录
版本 6.0 随 Visual Studio 2015 一起发布,发布了很多使得 C# 编程更有效率的小功能。 以下介绍了部分功能:
nameof
用于获取变量、类型或成员的简单(非限定)字符串名称。
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
string str = null;
int number = 123;
char ch = 'a';
Console.WriteLine(nameof(str));
Console.WriteLine(nameof(number));
Console.WriteLine(nameof(ch));
Console.ReadLine();
}
}
}
结果:

内插字符串
用 $ 来构造字符串,也即在字符串“""”前加入“$”符号。
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
var world = "World";
Console.WriteLine($"Hello {world}!");
Console.ReadLine();
}
}
}
结果:

注意:想要在内插字符串中包含大括号(“{” 或 “}”),请使用两个大括号,即 “{{” 或 “}}”。
NULL 条件运算符
用于在执行成员访问 (?.) 或索引 (?[) 操作之前,测试是否存在 NULL 值。
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
string str1 = null;
string str2 = "123";
Console.WriteLine($"str1 length={str1?.Length}");
Console.WriteLine($"str2 length={str2?.Length}");
Console.WriteLine($"str1[0]={str1?[0]}");
Console.WriteLine($"str2[0]={str2?[0]}");
Console.ReadLine();
}
}
}
结果:

这种方式在某些情况下可以省略判断是否为空的步骤,让他即便实在null的时候也能调用方法。
catch 和 finally 块中使用 await
现在可以在catch和finally块中使用await了。
using System;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
}
async Task Test()
{
MyTest myTest = new MyTest();
try
{
await myTest.show();
}
catch (Exception e)
{
await myTest.show();
}
finally
{
await myTest.show();
}
}
}
class MyTest
{
public async Task<string> show()
{
Console.WriteLine("hi C#");
return "null";
}
}
}
自动实现的属性
现在可以通过与初始化字段相似的方式来初始化自动属性。当属性访问器中不需要任何其他逻辑时,自动实现的属性会使属性声明更加简洁。
using System;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
MyTest myTest = new MyTest();
Console.WriteLine(myTest.Name);
Console.ReadLine();
}
}
class MyTest
{
public string Name { get; set; } = "五十六精研";
}
}
结果:

类似于:
class MyTest
{
public string Name { get; set; }
public MyTest()
{
Name = "五十六精研";
}
}
只有 getter 的自动属性
现在可以定义只读自动属性,而不必使用完整属性语法定义属性。可以在声明属性的位置或类型的构造函数中初始化属性。
class Person
{
//新写法
private string Name { get; } = "五十六精研"; //不用带上 private set;
//旧写法
public int Age { get; private set; } ;
}
具有表达式主体的函数成员
可以采用与用于 lambda 表达式相同的轻量语法,声明具有代码表达式主体的成员。
using System;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
MyTest myTest = new MyTest();
Console.WriteLine(myTest.sum(1,2));
myTest.show();
Console.ReadLine();
}
}
class MyTest
{
public string Name { get; set; } = "五十六精研";
public int sum(int x, int y) => x + y; //带返回值的方法
public void show() => Console.WriteLine("hi C#"); //不带返回值的方法
}
}
结果:

索引初始值设定项
可以初始化支持索引编制的集合的特定元素(如初始化字典)。如果集合支持索引,可以指定索引元素。
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
//新的写法
var dicts = new Dictionary<int, string>()
{
[1] = "123",
[2] = "456",
[3] = "789"
};
//旧的写法
var pastDicts = new Dictionary<int, string>()
{
{1,"123"},
{2,"456"},
{3,"789"}
};
Console.ReadLine();
}
}
}
using static 类型
可以导入静态类型的可访问静态成员,就能在不使用类型名限定访问的情况下使用成员。
using System;
using static System.Console; //使用静态引用
namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
WriteLine("Hello World");
Console.ReadLine();
}
}
}
结果:

using static 只能导入可访问的静态成员和指定类型中声明的嵌套类型,不会导入继承的成员。
异常过滤器
异常过滤器允许你在catch块执行前检查if条件
//新的写法
try
{
}
catch (Exception ex) if (ex.InnerException != null)
{
}
//旧的写法
try
{
}
catch (Exception ex)
{
if(ex.InnerException != null)
{
}
}
检查NULL值的条件访问操作符
using System;
using static System.Console;
namespace test
{
class Program
{
static void Main(string[] args)
{
string str = null;
WriteLine(str ?? "五十六精研");
Console.ReadLine();
}
}
}
结果:

“??”这里表示如果左边为null,那么结果就是右边。

1359

被折叠的 条评论
为什么被折叠?



