Export (0) Print
Expand All

Array.Clear Method

Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[SecuritySafeCriticalAttribute]
public static void Clear(
	Array array,
	int index,
	int length
)

Parameters

array
Type: System.Array
The Array whose elements need to be cleared.
index
Type: System.Int32
The starting index of the range of elements to clear.
length
Type: System.Int32
The number of elements to clear.

ExceptionCondition
ArgumentNullException

array is null.

IndexOutOfRangeException

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 the Array.

Reference-type elements are set to null. Boolean-type elements are set to false. Other value-type elements are set to zero.

The range of cleared elements wrap from row to row in a multi-dimensional array.

This method only clears the values of the elements; it does not delete the elements themselves. An Array has a fixed size; therefore, elements cannot be added or removed.

This method is an O(n) operation, where n is length.

The following example demonstrates the use of the Clear method with multi-dimensional arrays.


using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "One dimension (Rank=1):" + "\n";
      int[] numbers1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

      for (int i = 0; i < 9; i++)
      {
         outputBlock.Text += String.Format("{0} ", numbers1[i]);
      }
      outputBlock.Text += "\n";
      outputBlock.Text += "\n";

      outputBlock.Text += String.Format("Array.Clear(numbers1, 2, 5)") + "\n";
      Array.Clear(numbers1, 2, 5);

      for (int i = 0; i < 9; i++)
      {
         outputBlock.Text += String.Format("{0} ", numbers1[i]);
      }
      outputBlock.Text += "\n";
      outputBlock.Text += "\n";

      outputBlock.Text += "Two dimensions (Rank=2):" + "\n";
      int[,] numbers2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

      for (int i = 0; i < 3; i++)
      {
         for (int j = 0; j < 3; j++)
         {
            outputBlock.Text += String.Format("{0} ", numbers2[i, j]);
         }
         outputBlock.Text += "\n";
      }

      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Array.Clear(numbers2, 2, 5)") + "\n";
      Array.Clear(numbers2, 2, 5);

      for (int i = 0; i < 3; i++)
      {
         for (int j = 0; j < 3; j++)
         {
            outputBlock.Text += String.Format("{0} ", numbers2[i, j]);
         }
         outputBlock.Text += "\n";
      }

      outputBlock.Text += "Three dimensions (Rank=3):" + "\n";
      int[, ,] numbers3 = {{{1, 2}, {3, 4}},
                             {{5, 6}, {7, 8}},
                             {{9, 10}, {11, 12}}};

      for (int i = 0; i < 2; i++)
      {
         for (int j = 0; j < 2; j++)
         {
            for (int k = 0; k < 2; k++)
            {
               outputBlock.Text += String.Format("{0} ", numbers3[i, j, k]);
            }
            outputBlock.Text += "\n";
         }
         outputBlock.Text += "\n";
      }

      outputBlock.Text += String.Format("Array.Clear(numbers3, 2, 5)") + "\n";
      Array.Clear(numbers3, 2, 5);

      for (int i = 0; i < 2; i++)
      {
         for (int j = 0; j < 2; j++)
         {
            for (int k = 0; k < 2; k++)
            {
               outputBlock.Text += String.Format("{0} ", numbers3[i, j, k]);
            }
            outputBlock.Text += "\n";
         }
         outputBlock.Text += "\n";
      }
   }
}
/*  This code example produces the following output:
 * 
 * One dimension (Rank=1):
 * 1 2 3 4 5 6 7 8 9
 * 
 * Array.Clear(numbers1, 2, 5)
 * 1 2 0 0 0 0 0 8 9
 * 
 * Two dimensions (Rank=2):
 * 1 2 3
 * 4 5 6
 * 7 8 9
 * 
 * Array.Clear(numbers2, 2, 5)
 * 1 2 0
 * 0 0 0
 * 0 8 9
 * 
 * Three dimensions (Rank=3):
 * 1 2
 * 3 4
 * 
 * 5 6
 * 7 8
 * 
 * Array.Clear(numbers3, 2, 5)
 * 1 2
 * 0 0
 * 
 * 0 0
 * 0 8
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft