1. 程式人生 > >c#中類似JAVA中匿名內部類實現

c#中類似JAVA中匿名內部類實現

C#
 int[] arr = new int[2];
            //Array類有一個靜態方法Sort
            //引數是 Sort(Array,泛型引數的方法)
           
            Array.Sort(arr, delegate(int a, int b)
            {
                int a1 = arr[0];
                return a.CompareTo(b);
            });
 //net3的lamuda表示式
         Array.Sort(init, (a, b) => a.CompareTo(b));
 //net3的linq擴充套件方法
   var list= init.OrderBy(c=>c).ToArray();
//net3的linq查詢
  var list=from p in init orderby p select p;

Sort 方法

 public static void Sort<T>(T[] array, Comparison<T> comparison);

Comparison   在System名稱空間下的一個委託
namespace System
{
    // Summary:
    //     Represents the method that compares two objects of the same type.
    //
    // Parameters:
    //   x:
    //     The first object to compare.
    //
    //   y:
    //     The second object to compare.
    //
    // Type parameters:
    //   T:
    //     The type of the objects to compare.
    //
    // Returns:
    //     Value Condition Less than 0 x is less than y.  0 x equals y.  Greater than
    //     0 x is greater than y.
    public delegate int Comparison<T>(T x, T y);
}


JAVA中

    直接看一個執行緒介面 Runnable

new Runnable(){
		public void run()
		{
		}
	};