Generic List AddRange() Method
.Net framework generic list AddRange() method allow us to add the elements of the specified collection to the end of the List<T>.
the List class AddRange(T) method exists under System.Collections.Generic namespace. this method require to pass a parameter named 'collection'.
the 'collection' parameter value type is System.Collections.Generic.IEnumerable<T>.
the IEnumerable<T> represents the collection whose elements should be added to the end of the List<T>. this collection itself cannot be null, but it can contain elements that are null, if type 'T' is a reference type. the AddRange() method throw an exception named ArgumentNullException if the method passed collection is null. the collection elements order is preserved in the List<T>.
the following asp.net c# example code demonstrate us how can add a collection of elements to the end of a generic list in an asp.net application.
the IEnumerable<T> represents the collection whose elements should be added to the end of the List<T>. this collection itself cannot be null, but it can contain elements that are null, if type 'T' is a reference type. the AddRange() method throw an exception named ArgumentNullException if the method passed collection is null. the collection elements order is preserved in the List<T>.
the following asp.net c# example code demonstrate us how can add a collection of elements to the end of a generic list in an asp.net application.
GenericListAddRangeMethod.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("Orchid");
colors.Add("ForestGreen");
colors.Add("DodgerBlue");
colors.Add("YellowGreen");
List<string> redColors = new List<string>();
redColors.Add("Red");
redColors.Add("DarkRed");
redColors.Add("IndianRed");
colors.AddRange(redColors);
RadioButtonList1.DataSource = colors;
RadioButtonList1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Generic List AddRange() - How to add the elements of the specified collection to the end of List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
System.Collections.Generic.List AddRange() method
<br /> How to add the elements of the specified collection to the end of List
</h2>
<hr width="675" align="left" color="SkyBlue" />
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
BorderWidth="2"
BorderColor="Navy"
BorderStyle="Dashed"
Font-Names="Comic Sans MS"
Font-Italic="true"
ForeColor="Snow"
BackColor="MidnightBlue"
Width="450"
RepeatColumns="2"
>
</asp:RadioButtonList>
<br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Create Generic List And Add Elements And Populate RadioButtonList"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>

- Generic List Add() method
- Generic List Capacity property
- Generic List Clear() method
- Generic List Insert() method
- Generic List InsertRange() method
- Generic List LastIndexOf() method
- Generic List RemoveAt() method
- Generic List RemoveRange() method
- Generic List Reverse() method
- Generic List Reverse() method with range