Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check to see if a given object (reference or value type) is equal to its default

I'm trying to find a way to check and see if the value of a given object is equal to its default value. I've looked around and come up with this:

    public static bool IsNullOrDefault<T>(T argument)     {         if (argument is ValueType || argument != null)         {             return object.Equals(argument, default(T));         }         return true;     } 

The problem I'm having is that I want to call it like this:

            object o = 0;             bool b = Utility.Utility.IsNullOrDefault(o); 

Yes o is an object, but I want to make it figure out the base type and check the default value of that. The base type, in this case, is an integer and I want to know in this case if the value is equal to default(int), not default(object).

I'm starting to think this might not be possible.

like image 387
Brian Avatar asked Jul 01 '11 21:07

Brian


People also ask

What is the default value for an object of type object?

An instance variable can also be a variable of object type. For such variables, the default initial value is null.

Which of the following is a default value for objects of reference type?

Null. The default value of a reference type variable is null when they are not initialized.

How do you check if an object is a specific type?

You can check object type in Java by using the instanceof keyword. Determining object type is important if you're processing a collection such as an array that contains more than one type of object. For example, you might have an array with string and integer representations of numbers.

What is the default value for object type array?

In Java, objects initializations assume the default value "null". Save this answer.


2 Answers

In your example, your integer is boxed and therefore your T is going to be object, and the default of object is null, so that's not valuable to you. If the object is a value type, you could get an instance of it (which would be the default) to use as a comparison. Something like:

if (argument is ValueType) {    object obj = Activator.CreateInstance(argument.GetType());    return obj.Equals(argument); } 

You'd want to deal with other possibilities before resorting to this. Marc Gravell's answer brings up some good points to consider, but for a full version of your method, you might have

public static bool IsNullOrDefault<T>(T argument) {     // deal with normal scenarios     if (argument == null) return true;     if (object.Equals(argument, default(T))) return true;      // deal with non-null nullables     Type methodType = typeof(T);     if (Nullable.GetUnderlyingType(methodType) != null) return false;      // deal with boxed value types     Type argumentType = argument.GetType();     if (argumentType.IsValueType && argumentType != methodType)      {         object obj = Activator.CreateInstance(argument.GetType());         return obj.Equals(argument);     }      return false; } 
like image 129
Anthony Pegram Avatar answered Sep 18 '22 04:09

Anthony Pegram


if o is null, in a non-generic (object) method, you will have no access to the original type - and you can't do much about that.

Hence, the only time it matters is non-nullable value-types, so:

Type type = value.GetType(); if(!type.IsValueType) return false; // can't be, as would be null if(Nullable.GetUnderlyingType(type) != null) return false; // ditto, Nullable<T> object defaultValue = Activator.CreateInstance(type); // must exist for structs return value.Equals(defaultValue); 
like image 37
Marc Gravell Avatar answered Sep 20 '22 04:09

Marc Gravell