Action和Func泛型委托實際上就是一個.NET Framework預定義的委托,3.5引入的特性。基本涵蓋了所有常用的委托,所以一般不用用戶重新聲明。
Action系列泛型委托,是沒有返回參數的委托,最多可以有16參數,也可以沒有參數;Func系列的委托是有返回值的委托,最多可以有16個參數;
元組是C# 4.0引入的一個新特性,編寫的時候需要基於.NET Framework 4.0或者更高版本。元組使用泛型來簡化一個類的定義.
提供用於創造元組對象的靜態方法。最多可以提供創建新的 8 元組,即八元組。
Action
public delegate void Action()
//void method(); Action test1 = () => { Console.WriteLine("void method();"); }; //void method(x); Action<int> test2 = (x) => { Console.WriteLine("void method(t1)"); }; //void method(x, y); Action<int, int> test3 = (x, y) => { Console.WriteLine("void method(t1, t2)"); };
public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16>()
Func
public delegate TResult Func<out TResult>()
//int method(); Func<int> test4 = () => 10; //int method(x); Func<int, int> test5 = (x) => x * 2; //int method(x,x); Func<int, int, int> test6 = (x, y) => x * y;
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, out TResult>()
Tuple
public static Tuple<T1> Create<T1>( T1 item1 ) var tuple1 = Tuple.Create(1); Console.WriteLine(tuple1.Item1); // Displays 1 var tuple1 = new Tuple<int>(1); Console.WriteLine(tuple1.Item1); // Displays 1
Tuple.Create<T1, T2, T3, T4, T5, T6, T7, T8> 方法 (T1, T2, T3, T4, T5, T6, T7, T8) var primes = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int>(8));//元祖裡面可以嵌套元祖
Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>> test =
new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int, int, int>(8, 9));
//元祖准確的可以說是當做擁有多個返回值的一種方法,不用使用ref,out了,比較方便。