Action和Func泛型委托实际上就是一个.NET Framework预定义的委托,3.5引入的特性。基本涵盖了所有常用的委托,所以一般不用用户重新声明。
Action系列泛型委托,是没有返回参数的委托,最多可以有16参数,也可以没有参数;Func系列的委托是有返回值的委托,最多可以有16个参数;
元组是C# 4.0引入的一个新特性,编写的时候需要基于.NET Framework 4.0或者更高版本。元组使用泛型来简化一个类的定义.
提供用于创造元组对象的静态方法。最多可以提供创建新的 8 元组,即八元组。
Action
Console.WriteLine("======Action==========================================================");
//void method();
Action test1 = () => { Console.WriteLine("无参数"); };
test1();
//void method(x);
Action<int> test2 = (x) => { Console.WriteLine(x.ToString()); };
test2(555);
//void method(x, y);
Action<int, int> test3 = (x, y) => { Console.WriteLine(x + y); };
test3(1,2);
Func
Console.WriteLine("======Func==========================================================");
//int method();
Func<int> test4 = () => 10;
Console.WriteLine(test4());
//int method(x);
Func<int, int> test5 = (x) => x * 2;
Console.WriteLine(test5(1));
//int method(x,x);
Func<int, int, int> test6 = (x, y) => x * y;
Console.WriteLine(test6(1,2));
Console.ReadKey();
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了,比较方便。
Predicate
Predicate<int> pre1 = new Predicate<int>(PreTest1);
int[] arr = { 1, 2, 4, 5, 6, 77, 7, 8, 9 };
int[] first = Array.FindAll(arr, new Predicate<int>(
p =>
{
return p > 5;
}));
first.ToList().ForEach(new Action<int>(p => {Console.WriteLine(p);}));