I want to use 2 threads inside the foreach C#

Asked

Viewed 59 times

0

I don’t have much experience in C#. But I’m trying to understand the code of a program I have here.

Here use the following code:

foreach(var item in _main.entrada) {
....
}

I want to include _main.saida (along with input) in foreach.... has how to do this?

  • 1

    explain better... the foreach will go through item by item of a collection, of what you are needing ?

1 answer

0


I want to include _main.saida (along with input) in foreach.... has how to do this?

Yes, just use the method Union (is in the System.Linq namespace) provided the two collections are of the same type

foreach (var item in _main.entrada.Union(_main.saida))
{
    ...
}

Full example:

public class MainCollection
{
    public int[] entrada;

    public int[] saida;

    public MainCollection()
    {
        entrada = new int[] { 1, 2, 3 };
        saida = new int[] { 4, 5, 6 };
    }
}


public class Program
{
    private static MainCollection _main = new MainCollection();

    public static void Main()
    {
        foreach (var item in _main.entrada.Union(_main.saida))
        {
            Console.WriteLine(item);
        }

        Console.ReadKey();

        return;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.