委托系列05-Action/Func
一、为什么会有Action和Func
二、Action和Func的声明及区别
1、Action
1、普通委托的声明和调用
委托:
/// <summary>
/// 无参数无返回值委托
/// </summary>
public delegate void NoParamNoReturnDelegate();
/// <summary>
/// 有参数无返回值委托
/// </summary>
/// <param name="i"></param>
public delegate void WithParamNoReturnDelegate(int i);
方法:
/// <summary>
/// 无参数无返回值方法
/// </summary>
private void NoParamNoReturnFun()
{
}
/// <summary>
/// 有参数无返回值方法
/// </summary>
/// <param name="i"></param>
private void WithParamNoReturnFun(int i)
{
Console.WriteLine("This is DoNothing");
}
调用:
NoParamNoReturnDelegate NoParamNoReturnDelegate = new NoParamNoReturnDelegate(NoParamNoReturnFun);
NoParamNoReturnDelegate.Invoke();
WithParamNoReturnDelegate WithParamNoReturnDelegate = new WithParamNoReturnDelegate(WithParamNoReturnFun);
WithParamNoReturnDelegate.Invoke(10);
2、Actio委托声明和调用
Action action = new Action(NoParamNoReturnFun);
action.Invoke();
Action<int> action1 = new Action<int>(WithParamNoReturnFun);
action1.Invoke(10);
3、总结
从上图可以看得出来,下面两种方式都可以调用NoreturnNopara方法,效果是一样的。
ShowDelegate showDelegate = new ShowDelegate(NoreturnNopara);
showDelegate.Invoke();
Action action = new Action(NoreturnNopara);
action.Invoke();
2、Func
1、普通委托的声明和调用
委托:
/// <summary>
/// 无参数有返回值委托
/// </summary>
/// <returns></returns>
public delegate int NoParamWithReturnDelegate();
/// <summary>
/// 有参数有返回值委托
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public delegate int WithParamWithReturnDelegate(int i);
方法:
/// <summary>
/// 无参数有返回值方法
/// </summary>
/// <returns></returns>
private int NoParamWithReturnFun()
{
return 0;
}
/// <summary>
/// 有参数无返回值方法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
private int WithParamWithReturnFun(int i)
{
return 0;
}
调用:
//无参数有返回值
NoParamWithReturnDelegate NoParamReturnDelegate = new NoParamWithReturnDelegate(NoParamWithReturnFun);
NoParamReturnDelegate.Invoke();
//有参数有返回值
WithParamWithReturnDelegate WithParamWithReturnDelegate = new WithParamWithReturnDelegate(WithParamWithReturnFun);
WithParamWithReturnDelegate.Invoke(10);
1、Func委托的声明和调用
//无参数有返回值Func
Func<int> func1 = new Func<int>(NoParamWithReturnFun);
func1.Invoke();
//无参数有返回值Func
Func<int,int> func2 = new Func<int,int>(WithParamWithReturnFun);
func2.Invoke(10);
//多个参数的Func委托
Func<int, string, int> func3 = new Func<int, string, int>(DoNothingIntAndStringWithReturn);
func3.Invoke(10,"test");
Func<int, List<string>, DateTime, object, int, string, DateTime, object, int, string, DateTime, object, int, string, DateTime, object, int> func4 = null;
2、总结
从上图可以看得出来,下面两种方式都可以调用NoreturnNopara方法,效果是一样的。
//无参数有返回值
NoParamWithReturnDelegate NoParamReturnDelegate = new NoParamWithReturnDelegate(NoParamWithReturnFun);
NoParamReturnDelegate.Invoke();
//有参数有返回值
WithParamWithReturnDelegate WithParamWithReturnDelegate = new WithParamWithReturnDelegate(WithParamWithReturnFun);
WithParamWithReturnDelegate.Invoke(10);
//无参数有返回值Func
Func<int> func1 = new Func<int>(NoParamWithReturnFun);
func1.Invoke();
//无参数有返回值Func
Func<int,int> func2 = new Func<int,int>(WithParamWithReturnFun);
func2.Invoke(10);
3、区别
Action:
- 1.Action是来自于System.RunTime的一个声明好的可以带有一个或者多个参数的委托delegate
- 2.Action是没有返回值的委托
- 3.最多支持16个入参
- 4.想要支持更多的参数呢、---就可以自己些--也只是定义一个委托而已,只是多了一个参数
Func:
- 1.Func是来自于System.RunTime的一个声明好的可以有返回值的委托delegate,也可以有参数
- 2.Action是没有返回值的委托
- 3.尖括号中的第一个是输入参数,第二个是返回值
- 4.如果尖括号中只有一个参数,那就是返回值类型,没有输入参数
- 5.最多支持16个入参
- 6.想要支持更多的参数呢、---就可以自己些--也只是定义一个委托而已,只是多了一个参数
推荐这些文章:
/**
<p>给定一个数组 <code>nums</code>,编写一个函数将所有 <code>0</code> 移动到数组的末尾,同时保持非零元素的相对顺序。</p>
<p><strong>请注意</strong> ,必须在不复制数组的情况下原地对数组进行操作。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<p...
//方法存在多个参数,所有的参数前面必须加上@Param("id")注解
...
1.JavaDoc 生成文档
javadoc命令是用来生成自己API文档的
参数信息:
@author 作者名
@version版本号
@since指明需要最早使用的jdk版本
@param参数名
@return返回值情况
@throws异常抛出情况
...
一、代码准备
/// <summary>
/// 1.无参数无返回值委托
/// </summary>
public delegate void NoReturnNoParaOutClass();
public class CustomDelegate //: MulticastDelegate
{
/// <summary>
/// 2.无参数无返回值委托
/// </summary>
public delegate void No...
cnblog不愧是专业的程序员博客,还附带了metaweblog 功能。
这里简单记录一下,说不定以后用得上
metaweblog 是什么?
MetaWeblog API是Blog应用程序对外接口的国际规范标准。
通过MetaWeblog API,blog应用程序可以对外公布blog提供的服务,从而允许外面的程序能取得和设置blog文章的文本或属性。可使用该接口上传博文。
不多说了,操作几个接口试试
在哪看到自己的metaweblog地址
博客设置中,其他设置这里可以看到
点击去即可看到所支持的接口
注意接口是xmlrpc格式的传输方式
调用接口试试
xmlrpc的接口传输
这个是规...
1. <build>
1.1 <resources>
编译保留 *.propertie s,*.xml
1.2 <plugins>
拷贝config目录
Java 1.8 编译
一般打包(无Spring)
Spring 打包
2. <properties>
编码 编译版本
1. <build>
1.1 <resources>
编译保留 *.propertie s,*.xml
<resource>
<directory>src/main/java</dire...
//mvc服务
services.AddControllersWithViews(options => {
}).AddJsonOptions(a => a.JsonSerializerOptions.Converters.Add(new FzCyjh.YunSdkAJ.Common.DateTimeJsonConverter()));
/// <summary>
///
/// </summary>
public class DateTimeJsonConvert...
使用类做为Dictionary<T,K>的key需什么要求?
问题
<P> </P>
最佳回答
没有要求
...
想把域中的用户名和密码等信息取到,存如数据库,怎么办啊,没接触过。
问题
想把域中的用户名和密码等信息取到,存如数据库,怎么办啊,没接触过。要写C#的程序用。
最佳回答
#regionusing System;using System.Collections.Generic;using System.DirectoryServices;using System.Linq;#endregionnamespace Framework.Net.LDAP{ /// <summary> /// Class for helping with AD /// </summ...
【C#】 使用Gsof.Native 动态调用 C动态库
一、背景
使用C# 开发客户端时候,我们经常会调用一些标准的动态库或是C的类库。虽然C# 提供的PInvoke的方式,但因为使用的场景的多变,有些时候可能没办法,固定位置的调用,或是需要按需求调用不同的库。 设置当前目录的方式,有时会有加载不到的问题。CLI的方式又需要用C++做再次的封装。
二、说明
支持根据路径动态加载DLL类库
支持using 销毁加载的类库
调用方便
github: https://github.com/gaoshang212/gsof/tree/master/Gsof.Native
nuget: ht...
文章链接:https://www.dianjilingqu.com/51204.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。