重载 Sort, ==, !=

本文介绍了一个C#中的自定义类Items,该类实现了IComparable接口,并重写了相等运算符以实现对象之间的比较。通过具体属性如Item、File、IsScored和Weight来展示比较逻辑。

 

 

   class Items:IComparable
    {
        public string Item { get; set; }
        public string File { get; set; }
        public string IsScored { get; set; }
        public string Weight { get; set; }

        public int CompareTo(object obj)
        {
            if (obj is Items)
            {
                Items second = obj as Items;
                if (this.Item.CompareTo(second.Item) == 1)
                    return 1;
                else
                    return -1;
            }
            else
                return -1;
        }

        
        public static bool operator !=(Items first, Items second)
        {
            bool pass = true;
            if ((first.Item != second.Item) || (first.File != second.File) || (first.IsScored != second.IsScored) || (first.Weight != second.Weight))
                pass = true;
            else
                pass = false;
            return pass;
        }

        public static bool operator ==(Items first, Items second)
        {
            bool pass = true;
            if ((first.Item == second.Item) && (first.File == second.File) && (first.IsScored == second.IsScored) && (first.Weight == second.Weight))
                pass = true;
            else
                pass = false;
            return pass;

        }
    }

 

转载于:https://www.cnblogs.com/qixue/archive/2013/03/18/2966330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值