且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中,"{}"语法是什么意思?

更新时间:2023-01-24 13:24:26

从广义上讲,这是基于成员的模式匹配-例如:

In the more general sense, this is member-based pattern matching - for example:

if (foo is { Id: 42, Name: "abc"})
{

}

测试 foo 是否具有 Id 42和 Name "abc".在这种情况下,您正在测试零属性,因此它有效地 is object 相同(即,不是 null 的测试,或者是no-op true (用于非空值类型).

tests whether foo has Id 42 and Name "abc". In this case, you are testing zero properties, so it effectively becomes the same as is object (i.e. a not null test, or a no-op true for non-nullable value-types).

与问题中的内容进行比较( if(someObj == null))-它与该问题的相反,请注意,它也不会使用用于 null 测试的重载 == 运算符( == null 进行此测试).

To compare against what you have in the question (if (someObj == null)) - it is the opposite of that, noting that it will also not use an overloaded == operator for the null test (which == null will).