代码之家  ›  专栏  ›  技术社区  ›  Michael Kniskern

确定C中对象的值#

  •  1
  • Michael Kniskern  · 技术社区  · 16 年前

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

    编辑: 对象可以等于任何内置System.Value类型或引用类型。

    源代码:

    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
            }
        }
    }
    
    7 回复  |  直到 16 年前
        1
  •  3
  •   Cybis    16 年前

    这是怎么回事?

    public static bool IsZeroOrEmptyString(object obj)
    {
        if (obj == null)
            return false;
        else if (obj.Equals(0) || obj.Equals(""))
            return true;
        else
            return false;
    }
    
        2
  •  3
  •   Michael Kniskern    16 年前

    使用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;
    }
    
        3
  •  1
  •   Eoin Campbell    16 年前

    迈克尔,你需要在这里提供更多的信息。

    使用方法可以将字符串与null或string.empty进行比较

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

    int、小数、双精度(和其他数值类型)可以通过简单的==测试与0(零)进行比较

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

    您还可以通过使用?来拥有可以为空的值类型。当你实例化它们的时候。这允许您将值类型设置为空。

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

    对于任何其他对象,简单的==null测试将告诉您它是否为null

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

    希望能对事情有所启发。

        4
  •  1
  •   FlySwat    16 年前

    不太确定其背后的原因,因为.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);
            }
        }
    
        5
  •  0
  •   x0n    16 年前

    如果你说的是字符串,你的意思是空的还是string.empty?

    if(string.isNullOrEmpty(obj as string))…做某事

    • 莪相
        6
  •  0
  •   David Arno    16 年前

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

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

        7
  •  0
  •   yfeldblum    16 年前
    obj => obj is int && (int)obj == 0 || obj is string && (string)obj == string.Empty