码迷,mamicode.com
首页 > Windows程序 > 详细

C# 比较方法

时间:2014-10-24 12:52:13      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:blog   ar   div   on   art   log   ef   new   as   

        public int Compare(Product first, Product second) {
            return PartialComparer.RefernceCompare(first, second) ?? PartialComparer.Comare(first.Popularity, second.Popularity) ?? PartialComparer.Comare(first.Price, second.Price) ?? PartialComparer.Comare(first.Name, second.Name) ?? 0;
        }
    }

    public static class PartialComparer {
        public static int? Comare<T>(T first, T second) {
            return Compare(Comparer<T>.Default, first, second);
        }
        public static int? Compare<T>(IComparer<T> comparer, T first, T second) {
            int ret = comparer.Compare(first, second);
            return ret == 0 ? new int?() : ret;
        }
        public static int? RefernceCompare<T>(T first, T second) where T : class {
            return first == second ? 0 : (first == null ? -1 : (second == null ? 1 : new int?()));
        }
    }

  

C# 比较方法

标签:blog   ar   div   on   art   log   ef   new   as   

原文地址:http://www.cnblogs.com/alphafly/p/4047949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!