Array Clear() Method
.Net framework Array.Clear() method allow us to set a range of elements in an array to the default value
of each element type. the array Clear() method exists in System namespace. the Clear() method require to pass
three parameters named 'array', 'index' and 'length'.
The 'array' parameter value type is 'System.Array' which represents the array whose elements need to be cleared. 'index' parameter value data type is System.Int32. this integer value represents the starting index of the range of elements to clear. 'length' parameter value data type is also System.Int32. this integer value represents the number of elements to clear from array.
Array Clear() method throw ArgumentNullException, if the 'array' is null. it also throw IndexOutOfRangeException exception, if the 'index' is less than the lower bound of 'array' or 'length' is less than zero or the sum of 'index' and 'length' is greater than the size of 'array'.
This method sets the String elements to null, Boolean elements to false, DateTime to DateTime.Min. Clear() method resets each element in an array to the element type's default value.
The following asp.net c# example code demonstrate us how can we clear an array programmatically at run time in an asp.net application.
The 'array' parameter value type is 'System.Array' which represents the array whose elements need to be cleared. 'index' parameter value data type is System.Int32. this integer value represents the starting index of the range of elements to clear. 'length' parameter value data type is also System.Int32. this integer value represents the number of elements to clear from array.
Array Clear() method throw ArgumentNullException, if the 'array' is null. it also throw IndexOutOfRangeException exception, if the 'index' is less than the lower bound of 'array' or 'length' is less than zero or the sum of 'index' and 'length' is greater than the size of 'array'.
This method sets the String elements to null, Boolean elements to false, DateTime to DateTime.Min. Clear() method resets each element in an array to the element type's default value.
The following asp.net c# example code demonstrate us how can we clear an array programmatically at run time in an asp.net application.
ArrayClear.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
private string[] favoriteColorArray = { "SeaShell", "Crimson", "Silver", "SandyBrown" };
protected void Page_Load(object sender, System.EventArgs e) {
ListBox1.DataSource = favoriteColorArray;
ListBox1.DataBind();
}
protected void Button1_Click(object sender, System.EventArgs e)
{
Array.Clear(favoriteColorArray, 1, 1);
ListBox1.Items.Clear();
foreach (string element in favoriteColorArray)
{
ListItem li = new ListItem();
li.Text = element;
ListBox1.Items.Add(li);
}
Label1.Text = "Array clear [1,1] and repopulate ListBox";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to clear an array in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Fuchsia">asp.net array example: Array Clear</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="OrangeRed"
Font-Bold="true"
Font-Italic="true"
>
</asp:Label>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
ForeColor="DarkGreen"
Font-Bold="true"
Text="The ListBox populated by an array"
>
</asp:Label>
<br />
<asp:ListBox
ID="ListBox1"
runat="server"
BackColor="DarkGreen"
ForeColor="AliceBlue"
>
</asp:ListBox>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Font-Bold="true"
Text="Clear Array [1,1] And RePopulate ListBox"
ForeColor="DarkGreen"
/>
</div>
</form>
</body>
</html>


- How to sort an array elements
- How to reverse an array elements
- How to initialize an int array
- How to perform foreach loop through array elements
- How to initialize a two dimensional array
- How to initialize a two dimensional int array
- How to perform for loop through a two dimensional array elements
- How to get an element by index from a two dimensional array
- How to get an element by index from an array
- How to get index of an element from an array