C# - meaning of curly braces after the “is” operator

后端 未结 1 1144
面向向阳花
面向向阳花 2020-12-11 18:01

I found in some C# source code the following line:

if(!(context.Compilation.GetTypeByMetadataName(\"Xunit.FactAttribute\") is { } factAttribute))

<
相关标签:
1条回答
  • 2020-12-11 18:38

    This is a new pattern matching feature which was introduced in C# 8.0 and is called property pattern. In this particular case it is used to check that object is not null, example from linked article:

    static string Display(object o) => o switch
    {
        Point { X: 0, Y: 0 }         p => "origin",
        Point { X: var x, Y: var y } p => $"({x}, {y})",
        {}                           => o.ToString(),
        null                         => "null"
    };
    
    0 讨论(0)
提交回复
热议问题