C#  

Merge two arrays

Step 1: Design Page – MergeArrays.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MergeArrays.aspx.cs" Inherits="MergeArrays" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Merge Two Arrays - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            margin: 50px;
        }
        .container {
            width: 520px;
            margin: auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0px 0px 10px #ccc;
            padding: 25px;
        }
        h2 {
            color: #1A2A80;
            text-align: center;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            margin-top: 10px;
        }
        .btn {
            background-color: #7A85C1;
            color: white;
            border: none;
            padding: 10px;
            margin-top: 10px;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }
        .result {
            margin-top: 20px;
            font-weight: bold;
            color: #333;
            text-align: center;
            white-space: pre-line;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Merge Two Arrays</h2>

            <asp:Label ID="lblArray1" runat="server" Text="Enter First Array (comma-separated):"></asp:Label><br />
            <asp:TextBox ID="txtArray1" runat="server" CssClass="form-control" placeholder="Example: 1, 2, 3, 4"></asp:TextBox><br />

            <asp:Label ID="lblArray2" runat="server" Text="Enter Second Array (comma-separated):"></asp:Label><br />
            <asp:TextBox ID="txtArray2" runat="server" CssClass="form-control" placeholder="Example: 5, 6, 7, 8"></asp:TextBox><br />

            <asp:Button ID="btnMerge" runat="server" Text="Merge Arrays" CssClass="btn" OnClick="btnMerge_Click" /><br />

            <asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Backend Logic – MergeArrays.aspx.cs

using System;
using System.Linq;

public partial class MergeArrays : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnMerge_Click(object sender, EventArgs e)
    {
        string input1 = txtArray1.Text.Trim();
        string input2 = txtArray2.Text.Trim();

        if (string.IsNullOrEmpty(input1) || string.IsNullOrEmpty(input2))
        {
            lblResult.Text = "Please enter both arrays.";
            lblResult.ForeColor = System.Drawing.Color.Red;
            return;
        }

        try
        {
            // Convert comma-separated string into integer arrays
            int[] array1 = input1.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToArray();
            int[] array2 = input2.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToArray();

            // Manual merging (without built-in methods like Concat)
            int[] mergedArray = new int[array1.Length + array2.Length];
            int index = 0;

            // Copy elements of first array
            for (int i = 0; i < array1.Length; i++)
            {
                mergedArray[index] = array1[i];
                index++;
            }

            // Copy elements of second array
            for (int j = 0; j < array2.Length; j++)
            {
                mergedArray[index] = array2[j];
                index++;
            }

            lblResult.Text = $"First Array: {string.Join(", ", array1)}\n" +
                             $"Second Array: {string.Join(", ", array2)}\n\n" +
                             $"Merged Array: {string.Join(", ", mergedArray)}";
            lblResult.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblResult.Text = "Invalid input! Please enter only numbers separated by commas.";
            lblResult.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Real-Time Example Flow

  1. Open MergeArrays.aspx in your browser.

  2. Enter the following:

    • Array 1: 1, 2, 3, 4

    • Array 2: 5, 6, 7, 8

  3. Click “Merge Arrays”

  4. Output will be:

    First Array: 1, 2, 3, 4
    Second Array: 5, 6, 7, 8
    
    Merged Array: 1, 2, 3, 4, 5, 6, 7, 8
    

Explanation

StepDescription
1User inputs two arrays as comma-separated values.
2Convert both strings to integer arrays.
3Create a new array large enough to hold both arrays.
4Copy elements from both arrays manually using loops.
5Display all three arrays clearly on the screen.

Example Input / Output Table

First ArraySecond ArrayOutput (Merged Array)
1, 2, 34, 5, 61, 2, 3, 4, 5, 6
10, 2030, 40, 5010, 20, 30, 40, 50
5, 15, 2535, 455, 15, 25, 35, 45
100200, 300100, 200, 300

Algorithm

1. Read two arrays.
2. Create new array of size (array1.Length + array2.Length)
3. Copy elements of array1 into merged array.
4. Copy elements of array2 into merged array.
5. Display merged array.