C#-“是"后的花括号的含义操作员 [英] C# - meaning of curly braces after the "is" operator
本文介绍了C#-“是"后的花括号的含义操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在某些C#源代码中找到以下行:
I found in some C# source code the following line:
if(!(context.Compilation.GetTypeByMetadataName("Xunit.FactAttribute")是{} factAttribute))
这是另一个:
if(!(diagnostic.Location.SourceTree是{}树))
is
运算符后的花括号( {}
)是什么意思?
What is the meaning of the curly braces ({ }
) after the is
operator?
推荐答案
这是C#8.0中引入的新模式匹配功能,称为属性模式.在这种特殊情况下,它用于检查对象是否不为空,例如来自链接文章的示例:
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"
};
这篇关于C#-“是"后的花括号的含义操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文