为了账号安全,请及时绑定邮箱和手机立即绑定

通过匿名操作呼叫ForEach

通过匿名操作呼叫ForEach

C#
元芳怎么了 2021-05-05 17:34:59
编译此代码:           var arr = new[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };           arr.ForEach(x =>                    {                          Console.WriteLine(x);                    });失败:error CS7036: There is no argument given that corresponds to the required formal parameter 'action' of 'Array.ForEach<T>(T[], Action<T>)'为什么?
查看完整描述

1 回答

?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

ForEachArray该类的静态方法。但是,它不是扩展方法。

该方法的文档指出以下内容(强调我的意思):

指定数组的每个元素执行指定操作。

它带有两个参数:

  1. T[] -要在其元素上执行操作的一维,从零开始的数组。

  2. Action<T> -在数组的每个元素上执行的操作。

您需要这样称呼它:

Array.ForEach(arr, x =>

{

    Console.WriteLine(x);

});


查看完整回答
反对 回复 2021-05-29
  • 1 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信