예제 #1
0
        public static void Main(string[] args)
        {
            Person[] persons =
            {
                new Person {
                    LastName = "Huber", FirstName = "Herman", Age = 27
                },
                new Person {
                    LastName = "Adam", FirstName = "Paul", Age = 35
                },
                new Person {
                    LastName = "Mustermann", FirstName = "Max", Age = 42
                },
                new Person {
                    LastName = "Bertold", FirstName = "Brecht", Age = 19
                }
            };
            Console.WriteLine("Liste unsortiert");
            PrintOut(persons);

            MySort <Person> .Sort(persons);

            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach VORNAME aufsteigend!!!");
            PrintOut(persons);

            MySort <Person> .Sort(persons, new PersonAgeComparer());

            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach ALTER absteigend!!!");
            PrintOut(persons);
        }
예제 #2
0
        public void SortTest()
        {
            //Arrange
            string[] list = new string[5];
            list[0] = "Adolf";
            list[1] = "Maier";
            list[2] = "Müller";
            list[3] = "Huber";
            list[4] = "Burghard";
            //Act
            string[] index = { "Adolf", "Burghard", "Huber", "Maier", "Müller" };
            MySort <object> .Sort(list);

            int count = 0;

            foreach (var item in list)
            {
                int i = item.CompareTo(index[count++]);
                if (i != 0)
                {
                    break;
                }
            }
            //Assert
            Assert.AreEqual(list.ToString(), index.ToString());
        }
예제 #3
0
 public void SortTest()
 {
     //Arrange
     string[] list = new string[5];
     list[0] = "Adolf";
     list[1] = "Maier";
     list[2] = "Müller";
     list[3] = "Huber";
     list[4] = "Burghard";
     //Act
     string[] index = { "Adolf", "Burghard", "Huber", "Maier", "Müller" };
     MySort.Sort(list);
     //Assert
     CollectionAssert.AreEqual(list, index);
 }
예제 #4
0
 public void ComparePersonLastnameTest()
 {
     //Arrange
     Person[] list =
     {
         new Person("Maier",  "Helmut", 42),
         new Person("Müller", "Thomas", 33),
         new Person("Huber",  "Hans", 27)
     };
     //Act
     Person[] erwartet =
     {
         list[2], list[0], list[1]
     };
     MySort.Sort(list, new PersonLastNameComparer());
     //Assert
     CollectionAssert.AreEqual(list, erwartet);
 }
예제 #5
0
        public void CompareIntegerTest()
        {
            //Arrange
            MyList <object> list = new MyList <object>();

            list.Add(3);
            list.Add(4);
            list.Add(5);
            list.Add(0);
            list.Add(1);
            list.Add(2);
            object[] expected    = { 0, 1, 2, 3, 4, 5 };
            object[] targetArray = new object[6];
            //Act
            list.CopyTo(targetArray, 0);
            MySort.Sort(targetArray);
            //Assert
            CollectionAssert.AreEqual(expected, targetArray);
        }
예제 #6
0
        public void ComparePersonLastnameTest()
        {
            //Arrange
            Person[] list =
            {
                new Person("Maier",  "Helmut", 42),
                new Person("Müller", "Thomas", 33),
                new Person("Huber",  "Hans", 27)
            };
            //Act
            Person[] erwartet =
            {
                new Person("Huber",  "Hans",   27),
                new Person("Müller", "Thomas", 33),
                new Person("Maier",  "Helmut", 42)
            };
            MySort <object> .Sort(list, new PersonAgeComparer());

            //Assert
            Assert.AreEqual(list.ToString(), erwartet.ToString());
        }
예제 #7
0
        public static void Main(string[] args)
        {
            Person[] persons =
            {
                new Person {
                    LastName = "Karlsberger", FirstName = "Herman", Age = 27
                },
                new Person {
                    LastName = "Liszt", FirstName = "Franz", Age = 35
                },
                new Person {
                    LastName = "Mustermann", FirstName = "Max", Age = 42
                },
                new Person {
                    LastName = "Bertolt", FirstName = "Brecht", Age = 19
                },
                new Person("Maier", "Helmut", 42),
                new Person("Müller", "Thomas", 33),
                new Person("Strauss", "Johann", 27)
            };
            Console.WriteLine("Liste unsortiert");
            PrintOut(persons);

            MySort.Sort(persons);
            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach VORNAME aufsteigend!!!");
            PrintOut(persons);

            MySort.Sort(persons, new PersonAgeComparer());
            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach ALTER absteigend!!!");
            PrintOut(persons);

            MySort.Sort(persons, new PersonLastNameComparer());
            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach FAMILIENNAME aufsteigend!!!");
            PrintOut(persons);
        }
예제 #8
0
        static void Main(string[] args)
        {
            #region 计算器
            MyCalculate _MyCalculate = new MyCalculate();
            //一般的调用
            Calculate_Delegate _calculate_Delegate;
            //创建的多种方法
            _calculate_Delegate = new Calculate_Delegate(_MyCalculate.Add);
            //_calculate_Delegate = _MyCalculate.Add; //可以直接这么写(只要符合委托签名即可)
            //多播委托(栈方式 如果有返回值,则只返回后入的那个方法返回值)
            _calculate_Delegate += _MyCalculate.Subtraction;
            _calculate_Delegate += _MyCalculate.Multiplication;
            _calculate_Delegate -= _MyCalculate.Subtraction;
            _calculate_Delegate -= _MyCalculate.Divide;//可以去除不存在的方法
            double result;
            //执行的多种方法
            result = _calculate_Delegate.Invoke(4, 3);
            //Func<T> 委托
            result = _MyCalculate.DoCalculate <double>(4, 5, _MyCalculate.Add);
            Console.WriteLine($"{result}");
            result = _calculate_Delegate(4, 3);
            Console.WriteLine($"{result}");
            //方法作为参数传递给另一个方法
            result = _MyCalculate.DoCalculate(_MyCalculate.Divide, 4, 3);
            Console.WriteLine("计算结果是:" + result);

            #endregion

            #region 通用排序实例
            List <Employee> Employees = new List <Employee>
            {
                new Employee("和", 6),
                new Employee("林", 8),
                new Employee("粥", 5),
                new Employee("轮", 3),
                new Employee("顾", 9),
                new Employee("小", 4),
                new Employee("仟", 7)
            };

            MySort.Sort(Employees, Employee.CompareAge);
            Employees.ForEach(
                f =>
            {
                Console.WriteLine(f);
            });

            #endregion

            #region 多播委托和异常处理
            Action actions = One;
            actions += Two;
            ////第一种:这种多播委托 如果第一个方法异常了  就会停止迭代了  不会去执行后面的方法了
            try
            {
                actions.Invoke();
            }
            catch (Exception ex)
            {
                Console.WriteLine("actions.Invoke() exception");
            }
            //第二种:将多播委托使用GetInvocationList() ,得到一个Delegate[],再执行,出异常可以继续下一次迭代
            System.Delegate[] d = actions.GetInvocationList();
            foreach (Action item in d)
            {
                try
                {
                    item();
                }
                catch (Exception)
                {
                    Console.WriteLine("Delegate[] d , item() exception");
                }
            }

            #endregion

            //Action a = () => Console.WriteLine("1");
            Console.ReadKey();
        }