C# Program to merge sequences

In C#, you can merge sequences using the Zip method from LINQ. The Zip method combines elements from two sequences by pairing them together using a specified function.

Syntax

The Zip method syntax is −

sequence1.Zip(sequence2, (first, second) => result)

Parameters

  • sequence1 − The first sequence to merge

  • sequence2 − The second sequence to merge

  • resultSelector − A function that defines how to combine elements from both sequences

How It Works

The Zip method pairs elements from two sequences by their index position. If sequences have different lengths, the result contains only as many elements as the shorter sequence.

Zip Method Process Sequence 1 Sequence 2 1, 2, 3 "A", "B", "C" Zip Function "1A", "2B", "3C"

Using Zip to Merge Integer and String Arrays

Example

using System;
using System.Linq;

public class Demo {
    public static void Main() {
        int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
        string[] stringArray = { "Depp", "Cruise", "Pitt", "Clooney", "Sandler", "Affleck", "Tarantino" };
        
        var mergedSeq = intArray.Zip(stringArray, (one, two) => one + " " + two);
        
        foreach (var ele in mergedSeq) {
            Console.WriteLine(ele);
        }
    }
}

The output of the above code is −

1 Depp
2 Cruise
3 Pitt
4 Clooney
5 Sandler
6 Affleck
7 Tarantino

Using Zip with Different Data Types

Example

using System;
using System.Linq;

public class Program {
    public static void Main() {
        double[] prices = { 19.99, 29.99, 15.50, 45.00 };
        string[] products = { "Book", "Shirt", "Pen", "Laptop" };
        
        var inventory = prices.Zip(products, (price, product) => $"{product}: ${price}");
        
        Console.WriteLine("Product Inventory:");
        foreach (var item in inventory) {
            Console.WriteLine(item);
        }
    }
}

The output of the above code is −

Product Inventory:
Book: $19.99
Shirt: $29.99
Pen: $15.5
Laptop: $45

Using Zip with Unequal Length Sequences

Example

using System;
using System.Linq;

public class Program {
    public static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5 };
        char[] letters = { 'A', 'B', 'C' };
        
        var combined = numbers.Zip(letters, (num, letter) => $"{num}{letter}");
        
        Console.WriteLine("Combined result (stops at shorter sequence):");
        foreach (var item in combined) {
            Console.WriteLine(item);
        }
        
        Console.WriteLine($"Numbers length: {numbers.Length}");
        Console.WriteLine($"Letters length: {letters.Length}");
        Console.WriteLine($"Result length: {combined.Count()}");
    }
}

The output of the above code is −

Combined result (stops at shorter sequence):
1A
2B
3C
Numbers length: 5
Letters length: 3
Result length: 3

Conclusion

The Zip method in C# provides an efficient way to merge two sequences by combining elements at corresponding positions. It automatically handles sequences of different lengths by stopping when the shorter sequence ends, making it useful for pairing related data from multiple collections.

Updated on: 2026-03-17T07:04:35+05:30

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements