Generic List Clear() Method
.Net framework generic list Clear() method allow us to remove all elements from the List<T>. the Clear() method
exists in System.Collections.Generic namespace. this method require no parameter. the Clear() method implements as
ICollection<T>.Clear() and IList.Clear().
the Clear() method set the Count is 0 (zero) and references to other objects from elements of the collection are also released. this method does not change the list Capacity.
the following asp.net c# example code demonstrate us how can we remove all elements from the specified generic list in an asp.net application.
the Clear() method set the Count is 0 (zero) and references to other objects from elements of the collection are also released. this method does not change the list Capacity.
the following asp.net c# example code demonstrate us how can we remove all elements from the specified generic list in an asp.net application.
GenericListClearMethod.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("DarkOrchid");
colors.Add("Plum");
colors.Add("BlanchedAlmond");
Label1.Text = "List Elements....";
foreach(string color in colors)
{
Label1.Text +="<br />" + color;
}
colors.Clear();
Label1.Text += "<br /><br />After Call Clear() method: List Elements....";
foreach (string color in colors)
{
Label1.Text += "<br />" + color;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Generic List Clear() - How to Remove all elements from the List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkGreen; font-style:italic;">
System.Collections.Generic.List Clear() method
<br /> How to Remove all elements from the List
</h2>
<hr width="475" align="left" color="Pink" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="SeaGreen"
Font-Italic="true"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Test Generic List Clear() Method"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>
