c# - How to add a new item at the end of an existing array

Array append - add new item in end of existing array
The following asp.net c# example code demonstrate us how can we append a new element to an array elements collection programmatically at run time in an asp.net application. Appending an item to a collection indicate that new item will be added at the end of items collection. .Net framework's Array Class has no any direct method or property to append an element to array.

To append an element to an array, first we need to resize the array object by using Array.Resize() method. Here, we increase the array size to make a room for new element. Now, array's last element is our appended element.

We can set/specify value for this appended element as this way Array[Array.Length-1]="value". Array.Length-1 return array last element's index number, because array contain zero-based index. As a result, we append a new element with value to an array.
array-append.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        string[] birds = new string[]
        {
            "Northern Raven",
            "Barn Swallow",
            "Horned Lark"
        };

        Label1.Text = "birds array[" + birds.Length.ToString()+ "].........<br />";
        foreach(string s in birds)
        {
            Label1.Text += s + "<br />";
        }

        Array.Resize(ref birds, birds.Length + 1);
        birds[birds.Length - 1] = "Cape Grassbird";

        Label1.Text += "<br />after added new item birds array[" + birds.Length.ToString() + "].........<br />";
        foreach (string s in birds)
        {
            Label1.Text += s + "<br />";
        }

        Array.Resize(ref birds, birds.Length + 1);
        birds[birds.Length - 1] = "Common Chiffchaff";

        Label1.Text += "<br />after added new item birds array["+ birds.Length.ToString()+"].........<br />";
        foreach (string s in birds)
        {
            Label1.Text += s + "<br />";
        }
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# array append - add new item in end of existing array</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# array append example - add new
            <br /> item in end of existing array
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="add new item in end of existing array"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples