经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# 9 新特性 —— 增强的模式匹配
来源:cnblogs  作者:WeihanLi  时间:2021/1/4 9:51:09  对本文有异议

C# 9 新特性 —— 增强的模式匹配

Intro

C# 9 中进一步增强了模式匹配的用法,使得模式匹配更为强大,我们一起来了解一下吧

Sample

C# 9 中增强了模式匹配的用法,增加了 and/or/not 操作符,而且可以直接判断属性,来看一下下面的这个示例:

  1. var person = new Person();
  2. // or
  3. // string.IsNullOrEmpty(person.Description)
  4. if (person.Description is null or { Length: 0 })
  5. {
  6. Console.WriteLine($"{nameof(person.Description)} is IsNullOrEmpty");
  7. }
  8. // and
  9. // !string.IsNullOrEmpty(person.Name)
  10. if (person.Name is not null and { Length: > 0 })
  11. {
  12. if (person.Name[0] is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.')
  13. {
  14. }
  15. }
  16. // not
  17. if (person.Name is not null)
  18. {
  19. }

这里的代码使用 DnSpy 反编译之后的代码是下面这样的:

  1. Person person = new Person();
  2. string text = person.Description;
  3. bool flag = text == null || text.Length == 0;
  4. if (flag)
  5. {
  6. Console.WriteLine("Description is IsNullOrEmpty");
  7. }
  8. text = person.Name;
  9. bool flag2 = text != null && text.Length > 0;
  10. if (flag2)
  11. {
  12. char c = person.Name[0];
  13. if (c >= 'a')
  14. {
  15. if (c > 'z')
  16. {
  17. goto IL_8B;
  18. }
  19. }
  20. else if (c >= 'A')
  21. {
  22. if (c > 'Z')
  23. {
  24. goto IL_8B;
  25. }
  26. }
  27. else if (c != ',' && c != '.')
  28. {
  29. goto IL_8B;
  30. }
  31. bool flag3 = true;
  32. goto IL_8E;
  33. IL_8B:
  34. flag3 = false;
  35. IL_8E:
  36. bool flag4 = flag3;
  37. if (flag4)
  38. {
  39. }
  40. }
  41. bool flag5 = person.Name != null;
  42. if (flag5)
  43. {
  44. }

Switch

这不仅适用于 is 也可以在 switch 中使用

  1. switch (person.Age)
  2. {
  3. case >= 0 and <= 3:
  4. Console.WriteLine("baby");
  5. break;
  6. case > 3 and < 14:
  7. Console.WriteLine("child");
  8. break;
  9. case > 14 and < 22:
  10. Console.WriteLine("youth");
  11. break;
  12. case > 22 and < 60:
  13. Console.WriteLine("Adult");
  14. break;
  15. case >= 60 and <= 500:
  16. Console.WriteLine("Old man");
  17. break;
  18. case > 500:
  19. Console.WriteLine("monster");
  20. break;
  21. }

反编译后的代码:

  1. int age = person.Age;
  2. int num = age;
  3. if (num < 22)
  4. {
  5. if (num < 14)
  6. {
  7. if (num >= 0)
  8. {
  9. if (num > 3)
  10. {
  11. Console.WriteLine("child");
  12. }
  13. else
  14. {
  15. Console.WriteLine("baby");
  16. }
  17. }
  18. }
  19. else if (num > 14)
  20. {
  21. Console.WriteLine("youth");
  22. }
  23. }
  24. else if (num < 60)
  25. {
  26. if (num > 22)
  27. {
  28. Console.WriteLine("Adult");
  29. }
  30. }
  31. else if (num > 500)
  32. {
  33. Console.WriteLine("monster");
  34. }
  35. else
  36. {
  37. Console.WriteLine("Old man");
  38. }

More

可以看到有些情况下可以简化不少代码,尤其是 if 分支比较多的情况下使用上面 switch 这样的写法会清晰很多

但是如果只是 string.IsNullOrEmpty 这种代码最好还是不要写得这么骚了,小心要被同事吐槽了

炫技需谨慎,小心被 ...

Reference

原文链接:http://www.cnblogs.com/weihanli/p/14226240.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号