Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'is' operator cannot be applied to an operand of a static type

Tags:

c#

nullable

What does this error mean in this context?

if (value == null)
    return "";

if (value is Nullable && ((INullable)value).IsNull) //error on this line
    return "";

if (value is DateTime)
{
    if (((DateTime)value).TimeOfDay.TotalSeconds == 0)
        return ((DateTime)value).ToString ("yyyy-MM-dd");
    return ((DateTime)value).ToString ("yyyy-MM-dd HH:mm:ss");
}

I searched but didn't get any information on this error. I am trying this on Mono (2.10.8.1). This is a project actually meant for Windows, but when I tried to compile it in Monodevelop, I got this error.

like image 577
Animesh Avatar asked Jan 23 '26 05:01

Animesh


1 Answers

The problem is here:

if (value is Nullable

It thinks you're talking about the static class System.Nullable rather than the System.Nullable<T> struct.

Perhaps you meant:

if (value is INullable ...)

?

Note that if value is of compile-time type object, then it will never be a Nullable<T>, as boxing a null value would give a null reference, and boxing a non-null value would give a boxed value of the underlying type.

If you think there's still something else which you need to achieve, please specify what you're trying to do.

like image 198
Jon Skeet Avatar answered Jan 24 '26 22:01

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!