1

How would I loop through two arays using a foreach loop? I found this previously, but that's for PHP not c#

$images = array('image1', 'image2', ...);
$descriptions = array('description1', 'description2', ...);

foreach (array_combine($images, $descriptions) as $image => $desc) {
  echo $image, $desc;
}

my thought is to have something like the following

string[] ValueA = {1,2,3}
string[] ValueB = (a,b,c}

foreach(something here from ValueA && ValueB)
{
   methodNameHere(ValueA, ValueB); //method I am calling requires the two values
}
6
  • 1
    Why not just use a for loop or a while loop ? Commented Jun 25, 2015 at 15:19
  • You can't do that in c#. What exactly do you try to accomplish? Commented Jun 25, 2015 at 15:20
  • 1
    if each array is going to be the same size you can just use a for loop and get each index at the same spot and call the method that way? Commented Jun 25, 2015 at 15:24
  • Oh well, the closest you can get to PHP is using dictionaries, but I don't see how far you can go, a for loop with a flag can be absolutely enough. Commented Jun 25, 2015 at 15:25
  • basically I wanted to combine to sets of data , I can do this manually but I think it will be much better if done with an array so that it walks through each set of data Commented Jun 25, 2015 at 15:26

3 Answers 3

2

You will be Zip operation that come in .Net 4 in feature. This on link1 and link2 is description.

You will be right something like:

var alpha = new [] { A, B, C, D };
var day = new [] { "s", "s", "m", "t" };

var  alphasAndDays =  alpha.Zip(day, (n, w) => new { Alpha = n, Day = w });
foreach(var ad in  alphasAndDays)
{
   Console.WriteLine(aw.Alpha + aw.Day);
}
Sign up to request clarification or add additional context in comments.

1 Comment

gave it go didnt get expected results, please note inside foreach I am running methodNameHere(ValueA, ValueB) not concatenating the two
0

A simple reiteration can do that:

 class Program
{
    static void Main(string[] args)
    {
        string[] setA = new string[3] {"1", "2", "3"};
        string[] setB = new string[3] { "a", "b", "c" };

        foreach (string val1 in setA) {

            foreach (string val2 in setB) {

                Program test = new Program();
                String printer = test.concatString(val1, val2);

                Console.WriteLine(printer);

            }
        }

        Console.ReadLine();

    }

    public string concatString(string value1, string value2) {

         String value3 = value1 + value2;
         return value3;
    }
}

Comments

0
 int[] numbers = { 1, 2, 3, 4 };
            string[] words = { "one", "two", "three" };

            var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);

            foreach (var item in numbersAndWords)
                Console.WriteLine(item);

            // This code produces the following output: 

            // 1 one 
            // 2 two 
            // 3 three

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.