zoukankan      html  css  js  c++  java
  • 4.4.2 空合并操作符让比较不再痛苦

        class Program
        {
            static void Main(string[] args)
            {
                Console.ReadKey();
            }
            static int Compare(Product x, Product y)
            {
                return PartialComparer.Compare(x.Name, y.Name) ?? PartialComparer.Compare(x.Age, y.Age) ?? PartialComparer.Compare(x.Popularity, y.Popularity) ?? 0;
            }
        }
        public class Product
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Popularity { get; set; }
        }
        public static class PartialComparer
        {
            public static int? Compare<T>(T first, T second)
            {
                return Compare(Comparer<T>.Default, first, second);
            }
            public static int? Compare<T>(IComparer<T> compare, T first, T second)
            {
                int ret = compare.Compare(first, second);
                return ret == 0 ? new int?() : ret;
            }
            public static int? ReferenceCompare<T>(T first, T second) where T : class
            {
                return first == second ? 0 : first == null ? -1 : second == null ? 1 : new int?();
            }
        }
  • 相关阅读:
    一种想法
    识别link_text
    识别name
    识别id
    文件的读写
    条件和循环
    网站测试-功能测试小结
    拷贝
    #团队博客作业1-小组成员介绍
    软件测试基础-Homework1
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/9998243.html
Copyright © 2011-2022 走看看