C#での委任の使用

15132 ワード


1、依頼呼び出し方法

  
  
  
  
  1. class MathOperations  
  2.     {  
  3.         public static double MultiplyByTwo(double value)  
  4.         {  
  5.             return value * 2;  
  6.         }  
  7.         public static double Square(double value)  
  8.         {  
  9.             return value * value;  
  10.         }  
  11.     } 

  
  
  
  
  1. /// <summary>  
  2.     ///   
  3.     /// </summary>  
  4.     /// <param name="x"></param>  
  5.     /// <returns></returns>  
  6.     delegate double DoubleOp(double x);  
  7.  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             // ,  
  13.             DoubleOp[] operations = {  
  14.                                         new DoubleOp(MathOperations.MultiplyByTwo),                    
  15.                                         new DoubleOp(MathOperations.Square),  
  16.                                     };  
  17.             for (int i = 0; i < operations.Length; i++)  
  18.             {  
  19.                 Console.WriteLine("Using opertion[{0}]",i);  
  20.                 ProcessAndDisplayNumber(operations[i],2.0);  
  21.                 ProcessAndDisplayNumber(operations[i], 7.94);  
  22.                 ProcessAndDisplayNumber(operations[i],1.414);  
  23.                 Console.ReadLine();  
  24.             }  
  25.         }  
  26.  
  27.         /// <summary>  
  28.         ///  ( )  
  29.         /// </summary>  
  30.         ///  MultiplyByTwo  
  31.         /// <param name="action"></param>  
  32.         /// <param name="value"></param>  
  33.         static void ProcessAndDisplayNumber(DoubleOp action,double value)  
  34.         {  
  35.              double result = action(value);  
  36.              Console.WriteLine("Value is {0},  {1}",value,result);  
  37.          }  
  38.     } 

 


 
2、実現を依頼する泡立ちソート

  
  
  
  
  1. class Program  
  2.     {  
  3.         /// <summary>  
  4.         ///  ,  
  5.         /// </summary>  
  6.         public delegate bool CompareOp(object lhs, object rhs);  
  7.  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Employee[] emlpoyees = {   
  11.                                      new Employee("A",20000),  
  12.                                      new Employee("B",10000),  
  13.                                      new Employee("C",25000),  
  14.                                      new Employee("D",18000)  
  15.                                    };  
  16.             CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);  
  17.             //  
  18.             BubbleSorter.Sort(emlpoyees,employeeCompareOp);  
  19.  
  20.             for (int i = 0; i < emlpoyees.Length; i++)  
  21.             {  
  22.                 Console.WriteLine(emlpoyees[i].ToString());  
  23.             }  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27.  
  28.     /// <summary>  
  29.     ///   
  30.     ///   
  31.     /// </summary>  
  32.     class BubbleSorter  
  33.     {  
  34.         /// <summary>  
  35.         ///  RhsIsGreater  
  36.         /// </summary>  
  37.           
  38.         public static void Sort(object[] sortArray, Program.CompareOp gtMethod)  
  39.         {  
  40.             for (int i = 0; i < sortArray.Length; i++)  
  41.             {  
  42.                 for (int j = i + 1; j < sortArray.Length; j++)  
  43.                 {  
  44.                     if (gtMethod(sortArray[i], sortArray[j]))  
  45.                     {  
  46.                         object temp = sortArray[i];  
  47.                         sortArray[i] = sortArray[j];  
  48.                         sortArray[j] = temp;  
  49.                     }  
  50.                 }  
  51.             }  
  52.         }  
  53.     } 

  
  
  
  
  1. class Employee  
  2.     {  
  3.         public string strName;  
  4.         public decimal strSalary;  
  5.         public Employee(string strName, decimal strSalary)  
  6.         {  
  7.             this.strName = strName;  
  8.             this.strSalary = strSalary;  
  9.         }  
  10.  
  11.         public override string ToString()  
  12.         {  
  13.             return string.Format(strName + ":{0:C}", strSalary);  
  14.         }  
  15.  
  16.         /// <summary>  
  17.         ///  true  
  18.         /// </summary>  
  19.         /// <param name="lhs"></param>  
  20.         /// <param name="rhs"></param>  
  21.         /// <returns></returns>  
  22.         public static bool RhsIsGreater(object lhs, object rhs)  
  23.         {  
  24.             Employee empLhs = (Employee)lhs;  
  25.             Employee empRhs = (Employee)rhs;  
  26.             return (empLhs.strSalary > empRhs.strSalary) ? true : false;  
  27.         }  
  28.     }