"is" operator compilation warning

From the link :

The keyword is causes a compile-time warning if the expression is always known.

I tried to create an example:

class MyClass
{
    public void method(MyClass c)
    {
        if (c is MyClass)
        {
            //...
        }

        if (c is Object)
        {
            //...
        }
    }
}

But I do not receive any warnings. Why?

Can someone show me an example where I get a warning (because the expression is always true)?

It works for false.

+4
source share
1 answer

The statement iswill return false if the value null, so if you call method(null), it will not be included in either if-block.

However, if they MyClasswere actually struct(i.e. are not null), this will lead to a warning.

+7

Source: https://habr.com/ru/post/1532329/


All Articles