tags:

views:

1972

answers:

7

What would be the best way to determine if an object equals number zero (0) or string.empty in C#?

EDIT: The object can equal any built-in System.Value type or reference type.

Source Code:

public void MyMethod(object input1, object input2)
{
    bool result = false;
    object compare = new object();

    if(input != null && input2 != null)
    {
        if(input1 is IComparable && input2 is IComparable)
        {
            //do check for zero or string.empty
            //if input1 equals to zero or string.empty
            result = object.Equals(input2);

            //if input1 not equals to zero or string.empty
            result = object.Equals(input1) && object.Equals(input2); //yes not valid, but this is what I want to accomplish
        }
    }
}
A: 

Do you mean null or string.empty, if you're talking about strings?

if (String.IsNullOrEmpty(obj as string)) { ... do something }

  • Oisin
x0n
A: 

In the first case by testing if it is null. In the second case by testing if it is string.empty (you answered your own question).

I should add that an object can never be equal to 0. An object variable can have a null reference though (in reality that means the variable has the value of 0; there is no object in this case though)

David Arno
int i = 0; object o = i;
Jason Jackson
or just: object obj = 0;
Jason Jackson
David forgot about boxing.
FlySwat
I didn't forget about boxing. In order to perform the comparison obj == 0, the value has to be unboxed, for the object itself doesn't equal 0, the value it boxes contains 0.
David Arno
+1  A: 

Michael, you need to provide a little bit more information here.

strings can be compared to null or string.Empty by using the method

string x = "Some String"
if( string.IsNullOrEmpty(string input) ) { ... }

int, decimals, doubles (and other numeric value-types) can be compared to 0 (zero) with a simple == test

int x = 0;
if(x == 0) { ... }

You can also have nullable value-types also by using the ? operator when you instantiate them. This allows you to set a value type as null.

int? x = null;
if( !x.HasValue ) {  }

For any other object, a simple == null test will tell you if its null or not

object o = new object();
if( o != null ) { ... }

Hope that sheds some light on things.

Eoin Campbell
+1  A: 

Not quite sure the reasoning behind this, because .Equals is reference equality on reference types, and value equality on value types.

This seems to work, but I doubt its what you want:

    static bool IsZeroOrEmpty(object o1)
    {
        if (o1 == null)
            return false;
        if (o1.GetType().IsValueType)
        {                
            return (o1 as System.ValueType).Equals(0);
        }
        else
        {
            if (o1.GetType() == typeof(String))
            {
                return o1.Equals(String.Empty);
            }

            return o1.Equals(0);
        }
    }
FlySwat
When I was debugging, the "return (o1 as System.ValueType).Equals(0)" returned a false when I passed a zero to the method.....
Michael Kniskern
I would need to convert Zero "0" to the type of o1 to get a valid comparison, For example, my input is 0, is "System.Decimal" and the zero in the equals is considered a "System.Int32"
Michael Kniskern
A: 
obj => obj is int && (int)obj == 0 || obj is string && (string)obj == string.Empty
Justice
+2  A: 

What's wrong with this?

public static bool IsZeroOrEmptyString(object obj)
{
    if (obj == null)
        return false;
    else if (obj.Equals(0) || obj.Equals(""))
        return true;
    else
        return false;
}
Cybis
A: 

Using Jonathan Holland code sample with a minor modification, here is the solution that worked:

static bool IsZeroOrEmpty(object o1)
{
    bool Passed = false;
    object ZeroValue = 0;

    if(o1 != null)
    {
        if(o1.GetType().IsValueType)
        {
            Passed = (o1 as System.ValueType).Equals(Convert.ChangeType(ZeroValue, o1.GetType()))
        }
        else
        {
            if (o1.GetType() == typeof(String))
            {
                Passed = o1.Equals(String.Empty);
            }
        }
    }

    return Passed;
}
Michael Kniskern
That won't compile as there is a condition where it would not have a return.
FlySwat
I modified the source code to allow a valid compile
Michael Kniskern