C#中代理機制練習

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DelegateDemo01
{
    class BubbleSorter
    {
        public static void Sort<T>(IList<T> sortArray,Func<T,T,bool>comarison){
            bool swapped = true;
            do{
                swapped = false;
                for (int i = 0; i < sortArray.Count - 1;i++ ) {
                if(comarison(sortArray[i+1],sortArray[i])){
                    T temp = sortArray[i];
                    sortArray[i] = sortArray[i + 1];
                    sortArray[i + 1] = temp;
                    swapped = true;
                }
                }
            }while(swapped);
        
        }
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DelegateDemo01
{
    class Employee
    {
        //字段
        private string name;
        private decimal salary;
        //封裝屬性
        public string Name {
            get {
                return name;
            }
           private set {
                name = value;
            }
        }
        public decimal Salary {
            get {
                return salary;
            }
         private set {
                salary = value;
            }
        }
        //構造方法
        public Employee() { }
        public Employee(string name,decimal salary) {
            this.name = name;
            this.salary = salary;
        }
        //重寫父類的ToString 方法
        public override string ToString()
        {
            return string.Format("{0},{1:c}",Name,Salary);
        }
        //自定義比較方法
        public static bool CompareSalary(Employee e1,Employee e2) {
            return e1.Salary < e2.Salary;
        }


    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DelegateDemo01
{
    class Program
    {
       //定義委託類型。沒有方法體
        private delegate string GetString();
        delegate double DoubleOp(double x);
        //實例化委託類型,作爲參數傳入方法中
        static void ProcessAndDisplayNumber(DoubleOp action,double value) {
            double result = action(value);
            Console.WriteLine("Value is {0},result of operation is {1}",value,result);
        
        }
        static void Main(string[] args)
        {
            int x = 20;
            //實例化委託類型,傳入方法參數
            GetString getstr = new GetString(x.ToString);
            Console.WriteLine("委託結果:"+getstr());


            //委託數組使用
            DoubleOp[] doubleop = { 
                                  MathOperation.MultiplyByTwo,
                                  MathOperation.Square
                                  };


            for (int i = 0; i < doubleop.Length;i++ ) {
                Console.WriteLine("Using operations {0}:",i);
                ProcessAndDisplayNumber(doubleop[i],2.0);
                ProcessAndDisplayNumber(doubleop[i],3.45);
                ProcessAndDisplayNumber(doubleop[i],445.23);
                Console.WriteLine();
            
            }


            //委託的經典使用,冒泡排序
            Employee[] employee = { 
                                 new Employee("zhangsan",22222),
                                 new Employee("lisi",33212),
                                 new Employee("wanger",23444),
                                 new Employee("wanghai",33221)                                 
                                  };
            BubbleSorter.Sort(employee,Employee.CompareSalary);
            foreach(Employee em in employee){
                Console.WriteLine(em);
            }
            Console.ReadKey();
        }
    }
}

發佈了66 篇原創文章 · 獲贊 6 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章