c# - How to resize an array

Array Resize() Method
.Net framework Array.Resize<T> method allow us to change the number of elements of a one-dimensional array to the specified new size. the array Resize() method exists in System namespace. this method type parameter is 'T' which represents the type of the elements of the array.

the Resize() method has two required parameters named 'array' and 'newSize'. this 'array' parameter type is T[] which represents the one-dimensional, zero-based array to resize, or null to create a new array with the specified size. 'newSize' parameter type is System.Int32 which represents the size of the new array.

array Resize() method throw ArgumentOutOfRangeException exception, if the 'newSize' is less than zero.

'array' must be one-dimensional to successfully resize it. if 'newSize' is less than the Length of the old array, then the extra elements are ignored while resizing.

the following asp.net c# example code demonstrate us how can we resize an array programmatically at run time in an asp.net application.
array-resize.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        string[] colors = { "red", "blue", "skyblue", "white" };

        Label1.Text = "colors array length: " + colors.Length.ToString();
        Label1.Text += "<br />colors array.........<br />";
        foreach (string s in colors)
        {
            Label1.Text += s + "<br />";
        }

        Array.Resize(ref colors, 3);

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

        Array.Resize(ref colors, 5);
        colors[3] = "maroon";
        colors[4] = "magenta";

        Label1.Text += "<br />colors array length [after resize]: " + colors.Length.ToString();
        Label1.Text += "<br />colors array.........<br />";
        foreach (string s in colors)
        {
            Label1.Text += s + "<br />";
        }
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - array resize</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - array resize
        </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="test array resize"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples