首页 > Windows开发 > 详细

C# 比较方法

时间:2014-10-24 12:51:43      阅读:305      评论:0      收藏:0      [点我收藏+]
        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# 比较方法

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!