码迷,mamicode.com
首页 > Windows程序 > 详细

C#高级功能(三)Action、Func,Tuple

时间:2016-07-04 00:54:36      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

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了,比较方便。

 

 

C#高级功能(三)Action、Func,Tuple

标签:

原文地址:http://www.cnblogs.com/zhao123/p/5639082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!