Fuc和Action的使用
来源:互联网 发布:Python Max iteritem 编辑:程序博客网 时间:2024/06/14 07:18
平时我们如果要用到委托一般都是先声明一个委托类型,比如:
private delegate string Say();
string说明适用于这个委托的方法的返回类型是string类型,委托名Say后面没有参数,说明对应的方法也就没有传入参数。
写一个适用于该委托的方法:
public static string SayHello()
{
return "Hello";
}
最后调用:
static void Main(string[] args)
{
Say say = SayHello;
Console.WriteLine(say());
}
这里我们先声明委托,然后再将方法传给该委托。有没有办法可以不定义委托变量呢?
答案是肯定的,我们可以用Func.
Func是.NET里面的内置委托,它有很多重载。
Func<TResult>:没有传入参数,返回类型为TResult的委托。就像我们上面的Say委托,就可以用Func<string>来替代,调用如下:
static void Main(string[] args)
{
Func<string> say = SayHello;
//Say say = SayHello;
Console.WriteLine(say());
}
怎么样,有了Func很简单吧。看一下Func别的重载。
Func<T, TResult> 委托:有一个传入参数T,返回类型为TResult的委托。如:
//委托 传入参数类型为string,方法返回类型为int
Func<string, int> a = Count;
//对应方法
public int Count(string num)
{
return Convert.ToInt32(num);
}
Func<T1, T2, TResult> 委托:有两个传入参数:T1与T2,返回类型为TResult。
类似的还有Func(T1, T2, T3, TResult) 委托、Func(T1, T2, T3, T4, TResult) 委托等。用法差不多,都是前面为方法的传入参数,最后一个为方法的返回类型。
Func也可以与匿名方法一起使用如:
复制代码
public static void Main()
{
Func<string, int, string[]> extractMeth = delegate(string s, int i)
{
char[] delimiters = new char[] { ' ' };
return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
};
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extractMeth(title, 5))
Console.WriteLine(word);
}
复制代码
同样它也可以接 lambda 表达式
复制代码
public static void Main()
{
char[] separators = new char[] {' '};
Func<string, int, string[]> extract = (s, i) =>
i > 0 ? s.Split(separators, i) : s.Split(separators) ;
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extract(title, 5))
Console.WriteLine(word);
}
复制代码
Func都是有返回类型的,如果我们的方法没有返回类型该怎么办呢?铛铛铛,这时Action就要粉墨登场了。
Action 委托:没有传入参数,也没有返回类型,即Void。如:
复制代码
static void Main(string[] args)
{
Action say = SayHello;
say();
}
public static void SayHello( )
{
Console.WriteLine("Say Hello");
}
复制代码
Action<T> 委托:传入参数为T,没有返回类型。如:
复制代码
static void Main(string[] args)
{
Action<string> say = SayHello;
say("Hello");
}
public static void SayHello(string word )
{
Console.WriteLine(word);
}
复制代码
Action<T1, T2> 委托:两个传入参数,分别为T1与T2,没有返回类型。
Action同样的还有许多其它重载,每个重载用法一样,只是方法的传入参数数量不一样。
其实Action与Func的用法差不多,差别只是一个有返回类型,一个没有返回类型,当然Action也可以接匿名方法和Lambda表达式。
匿名方法:
复制代码
static void Main(string[] args)
{
Action<string> say = delegate(string word)
{
Console.WriteLine(word);
};
say("Hello Word");
}
复制代码
Lambda表达式:
static void Main(string[] args)
{
Action<string> say = s => Console.WriteLine(s);
say("Hello Word");
}
private delegate string Say();
string说明适用于这个委托的方法的返回类型是string类型,委托名Say后面没有参数,说明对应的方法也就没有传入参数。
写一个适用于该委托的方法:
public static string SayHello()
{
return "Hello";
}
最后调用:
static void Main(string[] args)
{
Say say = SayHello;
Console.WriteLine(say());
}
这里我们先声明委托,然后再将方法传给该委托。有没有办法可以不定义委托变量呢?
答案是肯定的,我们可以用Func.
Func是.NET里面的内置委托,它有很多重载。
Func<TResult>:没有传入参数,返回类型为TResult的委托。就像我们上面的Say委托,就可以用Func<string>来替代,调用如下:
static void Main(string[] args)
{
Func<string> say = SayHello;
//Say say = SayHello;
Console.WriteLine(say());
}
怎么样,有了Func很简单吧。看一下Func别的重载。
Func<T, TResult> 委托:有一个传入参数T,返回类型为TResult的委托。如:
//委托 传入参数类型为string,方法返回类型为int
Func<string, int> a = Count;
//对应方法
public int Count(string num)
{
return Convert.ToInt32(num);
}
Func<T1, T2, TResult> 委托:有两个传入参数:T1与T2,返回类型为TResult。
类似的还有Func(T1, T2, T3, TResult) 委托、Func(T1, T2, T3, T4, TResult) 委托等。用法差不多,都是前面为方法的传入参数,最后一个为方法的返回类型。
Func也可以与匿名方法一起使用如:
复制代码
public static void Main()
{
Func<string, int, string[]> extractMeth = delegate(string s, int i)
{
char[] delimiters = new char[] { ' ' };
return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
};
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extractMeth(title, 5))
Console.WriteLine(word);
}
复制代码
同样它也可以接 lambda 表达式
复制代码
public static void Main()
{
char[] separators = new char[] {' '};
Func<string, int, string[]> extract = (s, i) =>
i > 0 ? s.Split(separators, i) : s.Split(separators) ;
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extract(title, 5))
Console.WriteLine(word);
}
复制代码
Func都是有返回类型的,如果我们的方法没有返回类型该怎么办呢?铛铛铛,这时Action就要粉墨登场了。
Action 委托:没有传入参数,也没有返回类型,即Void。如:
复制代码
static void Main(string[] args)
{
Action say = SayHello;
say();
}
public static void SayHello( )
{
Console.WriteLine("Say Hello");
}
复制代码
Action<T> 委托:传入参数为T,没有返回类型。如:
复制代码
static void Main(string[] args)
{
Action<string> say = SayHello;
say("Hello");
}
public static void SayHello(string word )
{
Console.WriteLine(word);
}
复制代码
Action<T1, T2> 委托:两个传入参数,分别为T1与T2,没有返回类型。
Action同样的还有许多其它重载,每个重载用法一样,只是方法的传入参数数量不一样。
其实Action与Func的用法差不多,差别只是一个有返回类型,一个没有返回类型,当然Action也可以接匿名方法和Lambda表达式。
匿名方法:
复制代码
static void Main(string[] args)
{
Action<string> say = delegate(string word)
{
Console.WriteLine(word);
};
say("Hello Word");
}
复制代码
Lambda表达式:
static void Main(string[] args)
{
Action<string> say = s => Console.WriteLine(s);
say("Hello Word");
}
阅读全文
0 0
- Fuc和Action的使用
- Fuc委托的定义
- Fuc*360
- struts2 action发放的使用和通配符的使用
- Func和Action委托的区别和简单使用
- Func和Action委托的区别和简单使用
- Func和Action委托的区别和简单使用
- spark的算子Tranformation和Action的使用demo
- Action的使用
- s:action的使用
- iOS_target-action的使用
- action标签的使用
- Action Bar的使用
- Action接口的使用
- Struts2的Action使用
- 2.Action的使用
- interceptor和action使用同一个result的解决办法
- Struct1.x中Action和DispatchAction的使用
- [置顶] 基于深度学习的色情视频鉴定
- SSL2839 2017年11月6日提高组T1 游戏(dp)
- Hdu 6227 Rabbits
- 百度
- springmvc加载jsp
- Fuc和Action的使用
- kubernetes日志采集与解析
- java(十五):concurrent(0)—AtomicInteger,Unsafe,CAS
- win10 x64安装xgboost错误解决
- oracle:时区
- linux入门-python多版本切换
- HDU-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)
- JAVA同步之ReentrantLock可中断锁的使用
- Android --- 使用Intent传递对象的方式