Share via


Array.Clear Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sets a range of elements in the Array to zero, to false, or to nulla null reference (Nothing in Visual Basic), depending on the element type.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Sub Clear ( _
    array As Array, _
    index As Integer, _
    length As Integer _
)
[SecuritySafeCriticalAttribute]
public static void Clear(
    Array array,
    int index,
    int length
)

Parameters

  • index
    Type: System.Int32
    The starting index of the range of elements to clear.
  • length
    Type: System.Int32
    The number of elements to clear.

Exceptions

Exception Condition
ArgumentNullException

array is nulla null reference (Nothing in Visual Basic).

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.

Remarks

Reference-type elements are set to nulla null reference (Nothing in Visual Basic). 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.

Examples

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

' Visual Basic .NET versions 1.0 and 1.1 

Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= vbLf & "One dimension (Rank=1):" & vbCrLf
      Dim numbers1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9}

      For i As Integer = 0 To 8
         outputBlock.Text += String.Format("{0} ", numbers1(i))
      Next i
      outputBlock.Text &= vbCrLf

      outputBlock.Text += String.Format(vbLf & "Array.Clear(numbers1, 2, 5)") & vbCrLf
      Array.Clear(numbers1, 2, 5)

      For i As Integer = 0 To 8
         outputBlock.Text += String.Format("{0} ", numbers1(i))
      Next i
      outputBlock.Text &= vbCrLf


      outputBlock.Text &= vbLf & "Two dimensions (Rank=2):" & vbCrLf
      Dim numbers2(,) As Integer = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

      For i As Integer = 0 To 2
         For j As Integer = 0 To 2
            outputBlock.Text += String.Format("{0} ", numbers2(i, j))
         Next j
         outputBlock.Text &= vbCrLf
      Next i

      outputBlock.Text += String.Format(vbLf & "Array.Clear(numbers2, 2, 5)") & vbCrLf
      Array.Clear(numbers2, 2, 5)

      For i As Integer = 0 To 2
         For j As Integer = 0 To 2
            outputBlock.Text += String.Format("{0} ", numbers2(i, j))
         Next j
         outputBlock.Text &= vbCrLf
      Next i


      outputBlock.Text &= vbLf & "Three dimensions (Rank=3):" & vbCrLf
      Dim numbers3(,,) As Integer = {{{1, 2}, {3, 4}}, _
                                     {{5, 6}, {7, 8}}, _
                                     {{9, 10}, {11, 12}}}

      For i As Integer = 0 To 1
         For j As Integer = 0 To 1
            For k As Integer = 0 To 1
               outputBlock.Text += String.Format("{0} ", numbers3(i, j, k))
            Next k
            outputBlock.Text &= vbCrLf
         Next j
         outputBlock.Text &= vbCrLf
      Next i

      outputBlock.Text += String.Format("Array.Clear(numbers3, 2, 5)") & vbCrLf
      Array.Clear(numbers3, 2, 5)

      For i As Integer = 0 To 1
         For j As Integer = 0 To 1
            For k As Integer = 0 To 1
               outputBlock.Text += String.Format("{0} ", numbers3(i, j, k))
            Next k
            outputBlock.Text &= vbCrLf
         Next j
         outputBlock.Text &= vbCrLf
      Next i

   End Sub

End Module

' 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
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
 */

Version Information

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

Platforms

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