Object is not bool or equals false

I'm using C# 7.0 is type pattern. I'm trying to check if an object is not bool or the bool value equals false. However, the pattern I'm currently using with a bool type is:

if (obj is bool boolean && boolean)
{
    /* I'm not doing anything here */
}
else
{
    DoSomething();
}

Is there a way to invert this if expression using the same type pattern?

Upvotes: 0

Views: 192

Answers (1)

Paulo Morgado
Paulo Morgado

Reputation: 14846

You can also use a constant pattern:

if (!(obj is true))
{
    DoSomething();
}

Upvotes: 2

Related Questions