C# : 'is' keyword and checking for Not

后端 未结 12 2035
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 05:23

This is a silly question, but you can use this code to check if something is a particular type...

if (child is IContainer) { //....

Is ther

12条回答
  •  情书的邮戳
    2020-12-02 05:54

    C# 9 (released with .NET 5) includes the logical patterns and, or and not, which allows us to write this more elegantly:

    if (child is not IContainer) { ... }
    

    Likewise, this pattern can be used to check for null:

    if (child is not null) { ... }
    

提交回复
热议问题