简体   繁体   English

来自多个数组的foreach循环c#

[英]foreach loop from multiple arrays c#

This should be a simple question. 这应该是一个简单的问题。 All I want to know is if there is a better way of coding this. 我想知道的是,是否有更好的编码方式。 I want to do a foreach loop for every array, without having to redeclare the foreach loop. 我想为每个数组做一个foreach循环,而不必重新声明foreach循环。 Is there a way c# projects this? 有没有办法c#投射这个? I was thinking of putting this in a Collection...? 我想把它放在收藏中......?

Please, critique my code. 请批评我的代码。

        foreach (TextBox tb in vert)
        {
            if (tb.Text == box.Text)                
                conflicts.Add(tb);                
        }
        foreach (TextBox tb in hort)
        {
            if (tb.Text == box.Text)                
                conflicts.Add(tb);                
        }
        foreach (TextBox tb in cube)
        {
            if (tb.Text == box.Text)
                conflicts.Add(tb);                
        }

You can use LINQ: 您可以使用LINQ:

conflicts.AddRange(
    vert.Concat(hort).Concat(cube)
        .Where(tb => tb.Text == box.Text)
); 

I'm assuming that conflicts is a List<TextBox> , which has an AddRange method. 我假设conflictsList<TextBox> ,它有一个AddRange方法。 If it isn't, you'll need to call Add in a (single) loop. 如果不是,则需要在(单个)循环中调用Add
If you're creating conflicts , (or if it starts empty), you can call .ToList() instead. 如果您正在创建conflicts (或者如果它开始为空),则可以调用.ToList()

Another .net 3.5 approach:- 另一种.net 3.5方法: -

conflicts.AddRange(from textBox in vert.Concat(hort).Concat(cube)
                   where textBox.Text == box.Text
                   select textBox);

If you can't use LINQ for whatever reason (and I highly suggest you do) you could make your array searching a single method. 如果由于某种原因你不能使用LINQ(我强烈建议你这样做),你可以让你的数组搜索一个方法。 For example: 例如:

public void FindConflicts(IEnumerable<TextBox> tbList, IList<TextBox> conflicts, string test)
{
   foreach(TextBox tb in tbList)
   {
      if(tb.Text == test)
      {
          conflicts.Add(tb);
      }
   }
}

And then call it like so: 然后像这样调用它:

FindConflicts(vert, conflicts, box.Text);
FindConflicts(hort, conflicts, box.Text);
FindConflicts(cube, conflicts, box.Text);
var unionResult = vert.Concat(hort).Concat(cube)

foreach(TextBox tb in unionResult)
    if(tb.Text == box.Text)
        conflicts.Add(tb);

如果您使用.Net 3.5或更高版本,您应该能够使用Enumerable.Concat将它们粘合在一起

foreach (TextBox tb in vert.Concat(hort).Concat(cube))

There are of course many ways to write this, but you could also do 当然有很多方法可以写这个,但你也可以这样做

  foreach (var direction in new[] { vert, hort, cube })
    foreach (TextBox tb in direction)
      if (tb.Text == box.Text)
        conflicts.Add(tb);

If you try to create Sudoku game(mentioned in comments) first read about Permutation group and Combinatorics. 如果您尝试创建Sudoku游戏(在评论中提到),请先阅读有关Permutation组和Combinatorics的内容。 This will help you to choose more efficient Application Model w/o using foreach on text boxes. 这将帮助您在文本框中使用foreach选择更高效的应用程序模型。 Using lazy computation resolve the problem with object reduction but not improve your logics man. 使用延迟计算解决了对象减少的问题,但没有改善你的逻辑人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM