C# 确定C中对象的值#

C# 确定C中对象的值#,c#,casting,C#,Casting,确定对象是否等于C#中的数字零(0)或string.empty的最佳方法是什么 编辑:对象可以等于任何内置系统。值类型或引用类型 源代码: public void MyMethod(object input1, object input2) { bool result = false; object compare = new object(); if(input != null && input2 != null) { if(i

确定对象是否等于C#中的数字零(0)或string.empty的最佳方法是什么

编辑:对象可以等于任何内置系统。值类型或引用类型

源代码:

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

如果你说的是字符串,你是说null还是string.empty

如果(String.IsNullOrEmpty(obj as String)){…做点什么}

  • 奥辛

在第一种情况下,通过测试它是否为空。在第二种情况下,通过测试它是否为string.empty(您回答了自己的问题)


我应该补充一点,对象永远不能等于0。对象变量可以有一个空引用(实际上,这意味着变量的值为0;但在本例中没有对象)

Michael,您需要在这里提供更多的信息

通过使用方法,可以将字符串与null或string.Empty进行比较

string x = "Some String"
if( string.IsNullOrEmpty(string input) ) { ... }
int、小数、double(和其他数值类型)可以通过简单的==测试与0(零)进行比较

int x = 0;
if(x == 0) { ... }
您还可以使用?当您实例化它们时。这允许您将值类型设置为null

int? x = null;
if( !x.HasValue ) {  }
对于任何其他对象,一个简单的==null测试将告诉您它是否为null

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

希望这能给我们一些启示。

我不太清楚这背后的原因,因为.Equals是引用类型上的引用相等,而值类型上的值相等

这似乎有效,但我怀疑这是你想要的:

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

这个怎么了

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

使用Jonathan Holland代码示例并稍作修改,以下是有效的解决方案:

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

字符串不是值类型。这是一种参考类型。我没有忘记拳击。为了执行比较obj==0,必须取消对值的绑定,因为对象本身不等于0,它框中包含的值为0。在调试时,当我将零传递给方法时,“return(o1 as System.ValueType).Equals(0)”返回false…我需要将零“0”转换为o1的类型以获得有效的比较,例如,我的输入是0,是“System.Decimal”,equals中的0被认为是一个“System.Int32”,它不会编译,因为存在一个不会返回的条件。这也适用于我:
o1.equals(Convert.ChangeType(0,o1.GetType())
。只有当
obj
类型为Int32时,它才会返回
true
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;
}