通过匿名操作呼叫ForEach

编译此代码:


           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>)'

为什么?


元芳怎么了
浏览 136回答 1
1回答

拉莫斯之舞

ForEach是Array该类的静态方法。但是,它不是扩展方法。该方法的文档指出以下内容(强调我的意思):对指定数组的每个元素执行指定操作。它带有两个参数:T[]&nbsp;-要在其元素上执行操作的一维,从零开始的数组。Action<T>&nbsp;-在数组的每个元素上执行的操作。您需要这样称呼它:Array.ForEach(arr, x =>{&nbsp; &nbsp; Console.WriteLine(x);});
打开App,查看更多内容
随时随地看视频慕课网APP