tags:

views:

140

answers:

4
public static T IsNull<T>(object value, T defaultValue)
{
    return ((Object.Equals(value,null)) | (Object.Equals(value,DBNull.Value)) ?
        defaultValue : (T)value);
}

public static T IsNull<T>(object value) where T :new()
{
    T defaultvalue = new T();
    return IsNull(value, defaultvalue);
}

Have tested, and can use against data objects, classes and variables. Just want to know if there is better way to go about this.

+1  A: 

[Edited]

The following (non-generic) should work.

    public static bool IsNull(object value) 
    {
        return value == null;
    } 

Any value type will get boxed (i.e. non-null). Ref types will just be passed by pointer.

jdv
You're missing handling of value types.
Paul Ruane
Value types can't be null. For that, see uvita's answer.
spoulson
Obviously they can't, but OP's original method was general, this one is specific to class objects.
Paul Ruane
@Paul Ruane, spoulson. thanks for the comments. I still may be wrong as to what the intent is of the IsNull function though.
jdv
+8  A: 

It looks like you're trying to duplicate the null coalesce operator:

var foo = myPossiblyNullValue ?? defaultValue;
Randolpho
I love this operator, almost as much as the inline-if.
Codesleuth
This is actually the best way to do it. Although we will be comparing a value type with null if T is a struct or primitive, it does at least give the correct answer.
Paul Ruane
@Paul Ruane: keep in mind that the operator only works against nullable types. It makes no sense to even try to look for null on a non-nullable value type.
Randolpho
@Randolpho For non-nullable types, you can always use `Nullable.GetValueOrDefault(...)` http://msdn.microsoft.com/en-us/library/58x571cw.aspx Of course, this only works if you make your non-nullable type `Nullable<T>` http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
Dan Herbert
@Dan Herbert: you just contradicted yourself. You take your non-nullable type and make it nullable to fit your call to the method.
Randolpho
@Randolpho I forgot that only works for `Nullable<T>`. I updated my original comment to mention that. The reason why `GetValueOrDefault(...)` would be preferred however is because you can specify an alternate default value, which is what the original question's code was doing. Of course, the coalesce operator will do this as well...
Dan Herbert
@Dan Herbert: Exactly. The coalesce operator does everything needed. Plus it's succinct, and no utility code to maintain.
Randolpho
@Randolpho: my comment was actually against a different snippet. I'm not sure how your answer changed without leaving a history...
Paul Ruane
+2  A: 
public static bool IsNull<T>(object value)
{
    return object == default(T);
}
uvita
This will return true (is null) for the default value of every value, which is patently wrong as these values are not null.
Paul Ruane
Oh, sorry missed the return type.
Johannes Rudolph
Ok, now that I realize how bad this question has been formulated, I go for Konrad Rudolph's answer.
uvita
+5  A: 

First off, the method name is wrong. You imply that the result of the function is a boolean that is true exactly if the given value is null. In fact, that’s not the case. GetValueOrDefault might be a better name.

Secondly, you’re merely replicating the behaviour of the null coalesce operator, as mentioned by others.

Thirdly, your conditional is odd:

Object.Equals(value,null)) | (Object.Equals(value,DBNull.Value)

Why Object.Equals instead of ==? Better yet, use Object.ReferenceEquals since that makes it clear that you’re interested in reference equality. Also, you’re using the bitwise-or operator (|) which is semantically wrong in this context, although it happens to yield the right value. You want the boolean-or operator ||. (Also, inconsistency: why do you sometimes write object and other times Object?)

Finally, using type object instead of a generic type isn’t necessarily a good solution. It would be better to create overloads for generic reference and value types: this avoids boxing in the value types. It also means that you don’t have to specify the type explicitly in your second overload since it can be deduced from the method argument.

Konrad Rudolph
Excellent answer
Benjamin Podszun