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日

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?