Vanillaris 2022-11-13 00:59 采纳率: 50%
浏览 11
已结题

C#系统提供的委托函数的格式问题

问题遇到的现象和发生背景

【背景】Visual Studio2019(语言是C#)
【现象】系统提供的委托,和实现效果出现了相反的情况

用代码块功能插入代码,请勿粘贴截图

Func<string, int> a4 = (string value) =>
{
Console.WriteLine("有返回值有参数的lambed表达式{0}", value);
return 1;
};
Console.WriteLine(a4("123123"));

运行结果及报错内容

【运行结果】可以实现正常的效果,打印结果是 有返回值有参数的lambed表达式123123,然后第二行打印出来了“1”.
【但是】我在学习委托的时候,系统提供的Func ,生成的委托函数,尖括号里面的第一个参数应该是返回值类型吧,第二个位置和之后的位置才是参数的类型,但是这里的代码,如果说,a4 这个委托函数里面 只能装载 返回值为string类型,参数为 int类型的函数,我这里生成的Lambad表达式,他的参数是 string类型,返回值是int型,然后我在外面打印的时候,调用了 a4这个函数,此时我传进去的参数是 string ,但是 容器的要求不应该是 参数为int类型吗?
【而且】为什么这里明明顺序反了,程序也不会报错呢?并且我把 <string, int> 改成 <int,string>之后,反而报错了,我就更蒙了;

【这里放上我点击Func 然后 f12进去的内容】
// 摘要:
// Encapsulates a method that has one parameter and returns a value of the type
// specified by the TResult parameter.
//
// 参数:
// arg:
// The parameter of the method that this delegate encapsulates.
//
// 类型参数:
// T:
// The type of the parameter of the method that this delegate encapsulates.
//
// TResult:
// The type of the return value of the method that this delegate encapsulates.
//
// 返回结果:
// The return value of the method that this delegate encapsulates.
public delegate TResult Func<in T, out TResult>(T arg);

【谢谢】提前感谢大大的指导;

  • 写回答

1条回答 默认 最新

  • xuzuning 2022-11-13 09:17
    关注

    谁跟你说 尖括号里面的第一个参数应该是返回值类型 的呢?
    不要太想当然了
    The type of the parameter of the method that this delegate encapsulates
    此委托封装的方法的参数类型
    The return value of the method that this delegate encapsulates.
    此委托封装的方法的返回值。

    你的英语是谁教的?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    问题事件

    • 系统已结题 11月21日
    • 已采纳回答 11月13日
    • 创建了问题 11月13日

    悬赏问题

    • ¥30 深度学习的模型融合问题
    • ¥20 数电实验Verilog编程
    • ¥15 机器人MATLAB仿真示教占内存过大
    • ¥15 JSR233已经使用但是仍无法将csv数据放到一个list里显示出来,如何解决?
    • ¥15 I350 Gigabit Network
    • ¥15 关于#abap#的问题,请各位专家解答!
    • ¥20 内网通过公网访问外网问题
    • ¥20 谁有这个东西 继续教育的
    • ¥15 怎么使请求通过cors
    • ¥15 WDM 驱动ACPI 相关疑问