Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Foreach/For循环可选lambda函数?_C# - Fatal编程技术网

C# Foreach/For循环可选lambda函数?

C# Foreach/For循环可选lambda函数?,c#,C#,我们使用for或foreach循环遍历集合并处理每个条目 对于C#中的集合,在所有这些新的lambda函数中是否有其他选择? 传统做法 foreach(var v in vs) { Console.write(v); } 有类似的吗 vs.foreach(v => console.write(v)) 您所写的内容是完全合法的(除了案例)-假设vs的类型已将ForEach定义为扩展方法,或者如果它是列表 是为列表定义的: 对列表的每个元素执行指定的操作 对 对于IList,有一个Fo

我们使用for或foreach循环遍历集合并处理每个条目

对于C#中的集合,在所有这些新的lambda函数中是否有其他选择?

传统做法

foreach(var v in vs)
{
  Console.write(v);
}
有类似的吗

vs.foreach(v => console.write(v))

您所写的内容是完全合法的(除了案例)-假设
vs
的类型已将
ForEach
定义为扩展方法,或者如果它是
列表

是为
列表定义的

列表的每个元素执行指定的操作

对于IList
,有一个ForEach扩展方法:

列表有一个ForEach方法:

List<string> list = new List<string>() { "1", "2", "3", "4" };

list.ForEach(s => Console.WriteLine(s));
List List=newlist(){“1”、“2”、“3”、“4”};
list.ForEach(s=>Console.WriteLine);

这就是您要寻找的吗?

正是这样做的。

列表有ForEach方法,但是IEnumerable没有

关于这一点,有许多问题/答案。我认为它没有在IEnumerable中实现的主要原因是,枚举上的Linq“意味着”没有副作用,因为它是一种查询语言

Eric Lippert在他的博客上解释了他的想法


默认情况下,没有一个。
您可以定义自己的:

publicstaticvoidforeach(此IEnumerable源代码,Action-Action){
foreach(源中的T val)
行动(val);
}

.NET3.5的Linq没有Foreach,但提供了一个。我习惯于使用Linqbridge,因为它是.NET3.5框架的一部分

如果您认为它可以为您的程序带来清晰性,只需将其添加到扩展方法集合中即可

static class Helper
{
    public static IEnumerable<t> ForEeach<t>
        (this IEnumerable<t> source, Action<t> act)
    {
        foreach (T element in source) act(element);
        return source;
    }
}
静态类助手
{
公共静态可数foreach
(此IEnumerable来源,Action act)
{
foreach(源中的T元素)act(元素);
返回源;
}
}

埃里克提出了一个令人信服的论点,除了有一个ForEach for List,但只针对List。我有过无数次机会使用这种非常有用的扩展方法,同样也因为缺少IEnumerable的通用扩展方法而感到沮丧,所以我最终决定编写自己的扩展方法

在这样做的过程中,我更进一步,编写了一个一次性的、非常流畅的扩展方法,允许在每个项目上链接操作,并在该链中只执行一次操作,如果没有其他原因的话,这在当时似乎是个好主意

代码如下:

public static class EnumerableExtensions
{   
   public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
   {
      foreach (var item in collection)
      {
         action(item);
      }
   }

   public static IEnumerable<T> Once<T>(this IEnumerable<T> collection, 
      Func<IEnumerable<T>, bool> predicate, Action<IEnumerable<T>> action)
   {
      if (predicate(collection)) 
         action(collection);
      return collection;
   }

   public static IEnumerable<T> ForEvery<T>(this IEnumerable<T> collection,
      Func<T, bool> predicate, Action<T> action)
   {
      foreach (var item in collection)
      {
         if (predicate(item)) 
            action(item);
      }
      return collection;
   }
}
公共静态类EnumerableExtensions
{   
公共静态void ForEach(此IEnumerable集合,操作)
{
foreach(集合中的var项)
{
行动(项目);
}
}
公共静态IEnumerable Once(此IEnumerable集合,
Func谓词,动作(Action)
{
if(谓词(集合))
行动(收集);
回收;
}
公共静态IEnumerable ForEvery(此IEnumerable集合,
Func谓词,动作(Action)
{
foreach(集合中的var项)
{
if(谓语(项))
行动(项目);
}
回收;
}
}

您可以在我的博客文章中找到示例用法代码。

只是想澄清一下:它是
列表中的一个方法,而不是扩展方法。将ForEach添加到LINQ没有意义,但将其上移到
IList
:转到支持它的地方!:)嗯,我站着纠正。我一直认为它是IList的一个扩展,而不是具体实现的一个方法。呵呵关于off of,我编辑了你的答案,但很抱歉我不能编辑你的评论,请自便:-)不知道off of有什么大不了的。。有人想解释一下吗?@Ian P:英语方面,我不太清楚,我不是以英语为母语的人。但不知何故,如果你是一名程序员,你会开始有一种英语语言的感觉(off在那里没有使用)☺). 在我编辑的句子中,很难推断出“方法关闭”所传达的含义。而“关闭关闭”的句子是指“关闭关闭”之前的单词是一个动词,单词method绝对不是动词。也许这可以说明:这:。在一个有点相关的注释中:+1链接到Eric Lippert的博客,他解释了为什么这样做是个坏主意(而传统的
foreach
实际上更好)。可能重复
vs.ToList().ForEach(item => Console.WriteLine(item));
static class Helper
{
    public static IEnumerable<t> ForEeach<t>
        (this IEnumerable<t> source, Action<t> act)
    {
        foreach (T element in source) act(element);
        return source;
    }
}
public static class EnumerableExtensions
{   
   public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
   {
      foreach (var item in collection)
      {
         action(item);
      }
   }

   public static IEnumerable<T> Once<T>(this IEnumerable<T> collection, 
      Func<IEnumerable<T>, bool> predicate, Action<IEnumerable<T>> action)
   {
      if (predicate(collection)) 
         action(collection);
      return collection;
   }

   public static IEnumerable<T> ForEvery<T>(this IEnumerable<T> collection,
      Func<T, bool> predicate, Action<T> action)
   {
      foreach (var item in collection)
      {
         if (predicate(item)) 
            action(item);
      }
      return collection;
   }
}