C# : 'is' keyword and checking for Not

后端 未结 12 2037
没有蜡笔的小新
没有蜡笔的小新 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 06:04

    This hasn't been mentioned yet. It works and I think it looks better than using !(child is IContainer)

    if (part is IContainer is false)
    {
        return;
    }
    

    New In C# 9.0

    https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#logical-patterns

    if (part is not IContainer)
    {
        return;
    }
    

提交回复
热议问题