Michael Kniskern
Michael Kniskern

Reputation: 25260

Determine value of object in C#

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
        }
    }
}

Upvotes: 2

Views: 9420

Answers (7)

Michael Kniskern
Michael Kniskern

Reputation: 25260

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;
}

Upvotes: 3

Cybis
Cybis

Reputation: 9863

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;
}

Upvotes: 2

yfeldblum
yfeldblum

Reputation: 65435

obj => obj is int && (int)obj == 0 || obj is string && (string)obj == string.Empty

Upvotes: 0

FlySwat
FlySwat

Reputation: 175583

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);
        }
    }

Upvotes: 1

Eoin Campbell
Eoin Campbell

Reputation: 44268

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.

Upvotes: 1

David Arno
David Arno

Reputation: 43254

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)

Upvotes: 0

x0n
x0n

Reputation: 52420

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

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

  • Oisin

Upvotes: 0

Related Questions