Intro
最近几个版本的 C# 在语法中有很多的变化,有很多语法能够帮助我们大大简化代码复杂度,使得代码更加简洁,分享几个我觉得比较实用的可以让代码更加简洁的语法
Default literal expressions
在 C# 7.1 之后,我们可以使用 default 来代替一个类型的默认值,例如:
1
2
3
|
public void Test( string str = deault){} string str = default ; |
在之前的版本我们需要显式指定类型,如 default(string),就不需要写类型了,编译器会推断出类型
Target-Typed New Expression
在 C# 9 中,引入了 Target-Typed New Expression 语法,和上面的 default 类似,我们在创建对象的时候不再需要在编译器可以推断出类型的地方再写出类型了,这在有时候会很有用,尤其是在写一个类型非常复杂的字段的时候,我们就只需要声明一次就可以了,可以参考下面的示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// target-typed new expression //private static readonly Dictionary<string, Dictionary<string, string>> // Dictionary = new Dictionary<string, Dictionary<string, string>>(); private static readonly Dictionary< string , Dictionary< string , string >> Dictionary = new (); // array ReviewRequest[] requests = { new () { State = ReviewState.Rejected }, new (), new (), }; |
Named Tuple
从 C# 7 开始,我们可以使用 Named Tuple 来优化 Tuple 的使用,在之前的版本我们只能 Item1, Item2 这样去使用 Tuple 的 Value,但是这样很不好理解,尤其是在没有文档说明的情况下,可能每次都要去返回值的地方看一下究竟每一个元素代表什么,Named Tuple 出现了之后就相当于一个强类型的 Tuple,能够使得代码更好理解,tuple 元素的含义一目了然,举个栗子:
1
2
3
4
5
6
7
8
9
10
11
12
|
( string Alpha, string Beta) namedLetters = ( "a" , "b" ); Console.WriteLine($ "{namedLetters.Alpha}, {namedLetters.Beta}" ); ( int code, string msg) result = (1, "" ); private static ( int code, string msg) NamedTuple() { return (0, string .Empty); } var result = NamedTuple(); Console.WriteLine(result.code); |
Deconstruct
与 Named Tuple 同时出现的,我们可以在类中声明一个 Deconstruct 与 Constructor 相对应,只是 Constructor 是输入参数, Deconstruct 是输出参数,来看一个示例吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class Point { public Point( double x, double y) => (X, Y) = (x, y); public double X { get ; } public double Y { get ; } public void Deconstruct( out double x, out double y) => (x, y) = (X, Y); } var p = new Point(3.14, 2.71); ( double X, double Y) = p; |
上面的示例是官方文档的一个示例,来看一个我们实际在用的一个示例吧:
1
2
3
4
5
6
7
8
9
10
11
|
public class IdNameModel { public int Id { get ; set ; } public string Name { get ; set ; } public void Deconstruct( out int id, out string name) { id = Id; name = Name; } } |
多个返回值时,有的数据不关心可以使用 “_” 来表示丢弃返回值,示例如下:
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
26
27
28
29
30
31
32
33
34
35
36
|
using System; using System.Collections.Generic; public class Example { public static void Main() { var (_, _, _, pop1, _, pop2) = QueryCityDataForYears( "New York City" , 1960, 2010); Console.WriteLine($ "Population change, 1960 to 2010: {pop2 - pop1:N0}" ); } private static ( string , double , int , int , int , int ) QueryCityDataForYears( string name, int year1, int year2) { int population1 = 0, population2 = 0; double area = 0; if (name == "New York City" ) { area = 468.48; if (year1 == 1960) { population1 = 7781984; } if (year2 == 2010) { population2 = 8175133; } return (name, area, year1, population1, year2, population2); } return ( "" , 0, 0, 0, 0, 0); } } // The example displays the following output: // Population change, 1960 to 2010: 393,149 |
Pattern-Matching
模式匹配最早开始于 C# 7.1,最早的形式如:if(a is string str),这是最简单也是最经典的一个模式匹配,它结合了之前需要两句话才能完成的功能,可以翻译成:
1
2
|
var str = a as string ; if (str != null ) //... |
除了 if,我们在 switch 里也可以使用模式匹配的
1
2
3
4
5
6
7
8
9
10
11
12
13
|
void SwitchPattern( object obj0) { switch (obj0) { case string str1: Console.WriteLine(str1); break ; case int num1: Console.WriteLine(num1); break ; } } |
在 C# 9 中引入了逻辑运算符 and/or/not 使得模式匹配更为强大,来看一个判断是否是合法的 Base64 字符的一个方法的变化:
C# 9 之前的代码:
1
2
3
4
5
6
7
8
9
10
11
|
private static bool IsInvalid( char value) { var intValue = ( int )value; if (intValue >= 48 && intValue <= 57) return false ; if (intValue >= 65 && intValue <= 90) return false ; if (intValue >= 97 && intValue <= 122) return false ; return intValue != 43 && intValue != 47; } |
使用 C# 9 增强的模式匹配之后的代码:
1
2
3
4
5
6
7
8
9
10
11
|
private static bool IsInvalid( char value) { var intValue = ( int )value; return intValue switch { >= 48 and <= 57 => false , >= 65 and <= 90 => false , >= 97 and <= 122 => false , _ => intValue != 43 && intValue != 47 }; } |
是不是一下子清晰的很多~~
Switch Expression
Switch Expression 是 C# 8 引入的新特性,C# 9 有结合模式匹配做了进一步的增强,使得其功能更加强大,来看示例吧:
修改前的代码是这样的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
var state = ReviewState.Rejected; var stateString = string .Empty; switch (state) { case ReviewState.Rejected: stateString = "0" ; break ; case ReviewState.Reviewed: stateString = "1" ; break ; case ReviewState.UnReviewed: stateString = "-1" ; break ; } |
使用 switch expression 之后的代码如下:
1
2
3
4
5
6
7
8
|
var state = ReviewState.Rejected; var stateString = state switch { ReviewState.Rejected => "0" , ReviewState.Reviewed => "1" , ReviewState.UnReviewed => "-1" , _ => string .Empty }; |
是不是看起来简洁了很多,还有进一步的增加优化,来看下一个示例:
1
2
3
4
5
6
7
8
9
|
( int code, string msg) result = (0, "" ); var res = result switch { (0, _) => "success" , (-1, _) => "xx" , (-2, "" ) => "yy" , (_, _) => "error" }; Console.WriteLine(res); |
猜猜不同情况的输出的结果是什么样的,再自己试着跑一下结果看看是不是符合预期吧
Index Range
Index/Range 是 C# 8 引入的一个新特性,主要优化了对元组的操作,可以更方便的做索引和切片操作
之前有过一篇详细的介绍文章,可以参考:C# 使用 Index 和 Range 简化集合操作
我们可以通过 ^(hat) 操作符来反向索引数组中的对象,可以通过 .. 来创建一个集合的子集合,来看一个简单的示例:
1
2
3
4
5
|
var arr = Enumerable.Range(1, 10).ToArray(); Console.WriteLine($ "last element:{arr[^1]}" ); var subArray = Enumerable.Range(1, 3).ToArray(); Console.WriteLine(arr[..3].SequenceEqual(subArray) ? "StartWith" : "No" ); |
Record
Record 是 C# 9 引入的新特性,record 是一个特殊的类,编译器会帮助我们做很多事情,会自动实现一套基于值的比较,而且可以很方便实现对象复制的功能,详细介绍可以参考之前的 record 介绍文章 C# 9 新特性 — record 解读,可以看下面这个简单的示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public abstract record Person( string FirstName, string LastName); public record Teacher( string FirstName, string LastName, int Grade) : Person(FirstName, LastName); public record Student( string FirstName, string LastName, int Grade) : Person(FirstName, LastName); public static void Main() { Person teacher = new Teacher( "Nancy" , "Davolio" , 3); Person student = new Student( "Nancy" , "Davolio" , 3); Console.WriteLine(teacher == student); // output: False Student student2 = new Student( "Nancy" , "Davolio" , 3); Console.WriteLine(student2 == student); // output: True } |
Top-Level Statement
Top-Level statement 是 C# 9 支持的新特性,我们可以不写 Main 方法,直接写方法体,对于一些比较简单的小工具,小测试应用来说会比较方便
1
2
|
using static System.Console; WriteLine( "Hello world" ); |
More
除了上面这些新特性,你觉得还有哪些比较实用的新特性呢,欢迎留言一起讨论哈~
References
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7
https://github.com/WeihanLi/SamplesInPractice/blob/master/CSharp9Sample/CleanCodeSample.cs
总结
到此这篇关于如何利用现代化C#语法简化代码的文章就介绍到这了,更多相关C#简化代码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://mp.weixin.qq.com/s/_MlkDLmQD7U-BYOgNDlGSg