Generic List Capacity property in asp.net c#

Get and set generic list capacity
.Net framework generic list Capacity property allow us to get or set the total number of elements the internal data structure can hold without resizing. the List class Capacity property exists in System.Collections.Generic namespace. this property return value data type is System.Int32 which value represent the number of elements that the List<T> can contain before resizing is required.

the Capacity property has two exceptions. it throw ArgumentOutOfRangeException if Capacity is set to a value that is less than Count. this property throw the OutOfMemoryException if there is not enough memory available on the system. List<T> capacity is always greater than or equal to Count. if Count exceeds Capacity while adding new elements, the Capacity automatically increased by reallocating internal array.

the following asp.net c# example code demonstrate us how can we get or set the generic list Capacity programmatically at run time in an asp.net application.
GenericListCapacity.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        List<string> colors = new List<string>();
        colors.Add("Snow");
        colors.Add("DodgerBlue");
        colors.Add("Magenta");
        colors.Add("OliveDrab");

        Label1.Text = "List Capacity: " + colors.Capacity.ToString();

        colors.Capacity = 25;
        Label1.Text += "<br />After Set New Value Now List Capacity: " + colors.Capacity.ToString();
        
        RadioButtonList1.DataSource = colors;
        RadioButtonList1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List Capacity Property - How to get or set the total number of elements the internal data structure can hold without resizing</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkRed; font-style:italic;">
            System.Collections.Generic.List Capacity Property
            <br /> How to get or set the total number of elements
            <br /> the internal data structure can hold without resizing
        </h2>
        <hr width="500" align="left" color="DeepPink" />
        <asp:Label
             ID="Label1" 
             runat="server"
             ForeColor="ForestGreen"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:RadioButtonList
             ID="RadioButtonList1"
             runat="server"
             BorderWidth="2"
             BorderColor="Crimson"
             BorderStyle="Solid"
             Font-Names="Comic Sans MS"
             Font-Italic="true"
             ForeColor="Snow"
             BackColor="Pink"
             Width="550"
             RepeatColumns="2"
             >
        </asp:RadioButtonList>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Create Generic List And Get Set List Capacity"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples