Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Code Simplification Query: The Sequential Foreach Loops

Tags:

c#

Suppose I have some code that looks like this:

foreach(type x in list y)
{
   //dostuff1(x)
}

foreach(type x in list y)
{
   //dostuff2(x)
}

foreach(type x in list y)
{
   //dostuff3(x)
}

foreach(type x in list y)
{
   //dostuff4(x)
}

foreach(type x in list y)
{
   //dostuff5(x)
}

I cannot combine things into one big for loop like this:

foreach (type x in list y)
{
    //dostuff1(x)
    //dostuff2(x)
    //dostuff3(x)
    //dostuff4(x)
    //dostuff5(x)
}

Doing so would change the order. Any commentary on the best ways to make the code simpler in C#?

I imagine I could solve this problem by creating a function like this, though I'd rather leave it the way it is than force future readers of my code to understand yield:

void func(type x)
{
    dostuff1(x)
    yield 0;
    dostuff2(x)
    yield 0;
    dostuff3(x)
    yield 0;
    dostuff4(x)
    yield 0;
    dostuff5(x)
    yield break;
}

for (int i = 0; i<5; ++i)
{
   foreach (type x in list y)
   {
       //Call func(x) using yield semantics, which I'm not going to look up right now
   }
}
like image 301
Brian Avatar asked Mar 20 '09 16:03

Brian


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


3 Answers

Another alternative:

List<Action<Foo>> actions = new List<Action<Foo>> { 
    doStuff1, doStuff2, doStuff3, doStuff4, doStuff5
};

foreach (Action<Foo> action in actions)
{
    foreach (Foo x in list)
    {
        action(x);
    }
}

Just checked, and that works. For instance:

using System;
using System.Collections.Generic;

public class Test
{
    static void Main(string[] args)
    {
        var actions = new List<Action<string>> {
            First, Second
        };

        foreach (var action in actions)
        {
            foreach (string arg in args)
            {
                action(arg);
            }
        }
    }

    static void First(string x)
    {
        Console.WriteLine("First: " + x);
    }

    static void Second(string x)
    {
        Console.WriteLine("Second: " + x);
    }
}

Results of running Test.exe a b c

First: a
First: b
First: c
Second: a
Second: b
Second: c
like image 116
Jon Skeet Avatar answered Oct 11 '22 10:10

Jon Skeet


If you have a fairly constant list of actions, you could just avoid the foreach loops, but still do the actions explicitly (haven't tested the code):

list.ForEach(action1);
list.ForEach(action2);
list.ForEach(action3);
list.ForEach(action4);
like image 31
Grzenio Avatar answered Oct 11 '22 12:10

Grzenio


Jon Skeet's answer is excellent (I just voted it up). Here's an idea to take it one step further:

If you do this a lot, you could make an extension method called "DoActionsInOrder" (or maybe you can come up with a better name) that does this. Here's the idea:

public static void DoActionsInOrder<T>(this IEnumerable<T> stream, params Action<T> actionList)
{
     foreach(var action in actionList)
     {
          foreach(var item in stream)
          {
               action(item);
          }
     }
}

Then, you could call it like this:

myList.DoActionsInOrder(doStuff1, doStuff2, doStuff3, doStuff4, doStuff5);
like image 25
Charlie Flowers Avatar answered Oct 11 '22 10:10

Charlie Flowers