C# 模式匹配

 最近在使用vs编码时,重构提示:模式匹配

Element view = bindable as Element;
            if (view == null)
            {
                return;
            }

运用模式匹配可以简写为:

 if (!(bindable is Element view))
            {
                return;
            }

模式匹配在C# 7.0中引入

https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-7#pattern-matching

在8.0中又有增强。 

C# 8.0中的模式匹配

https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8#more-patterns-in-more-places

 

posted @ 2019-06-21 17:41  peterYong  阅读(2719)  评论(0编辑  收藏  举报