dimanche 23 octobre 2016

How to check if boxed value is empty in .NET

For example,

I have input parameter of type object. And I know that this parameter can store one of value type int, float, double(boxed value) etc. But I don't know which value type will come to this method. And I want check if boxed value type empty or not.

Like this piece of code:

bool IsEmpty(object boxedProperty)
{
    return boxedProperty == default(typeof(boxedProperty))
}

I understand, I can do this:

bool IsEmpty(object boxedProperty)
{
    return boxedProperty is int && boxedProperty == default(int)
    || boxedProperty is float ....
}

But it looks like dirty solution. How do this better?





Aucun commentaire:

Enregistrer un commentaire