How to combine two arrays in C#

1 Answer

0 votes
using System;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        var numbers = new [] { 1, 2, 3, 4, 5 };
        var words = new [] { "one", "two", "three", "four", "five" };

        var numbers_words = numbers.Zip(words, (n, w) => new { Number = n, Word = w });
        
        foreach(var item in numbers_words) {
            Console.WriteLine(item.Number + " " + item.Word);
        }
    }
}



/*
run:

1 one
2 two
3 three
4 four
5 five

*/

 



answered Mar 11, 2024 by avibootz
...