Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
Assembly: mscorlib (in mscorlib.dll)
Public Shared Sub Clear ( _ array As Array, _ index As Integer, _ length As Integer _ )
public static void Clear( Array array, int index, int length )
public: static void Clear( Array^ array, int index, int length )
static member Clear : array:Array * index:int * length:int -> unit
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.
| Exception | Condition |
|---|---|
| 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.
Imports System Imports Microsoft.VisualBasic ' Visual Basic .NET versions 1.0 and 1.1 Module Example Sub Main() Console.WriteLine(vbLf & "One dimension (Rank=1):") Dim numbers1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9} For i As Integer = 0 To 8 Console.Write("{0} ", numbers1(i)) Next i Console.WriteLine() Console.WriteLine(vbLf & "Array.Clear(numbers1, 2, 5)") Array.Clear(numbers1, 2, 5) For i As Integer = 0 To 8 Console.Write("{0} ", numbers1(i)) Next i Console.WriteLine() Console.WriteLine(vbLf & "Two dimensions (Rank=2):") 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 Console.Write("{0} ", numbers2(i, j)) Next j Console.WriteLine() Next i Console.WriteLine(vbLf & "Array.Clear(numbers2, 2, 5)") Array.Clear(numbers2, 2, 5) For i As Integer = 0 To 2 For j As Integer = 0 To 2 Console.Write("{0} ", numbers2(i, j)) Next j Console.WriteLine() Next i Console.WriteLine(vbLf & "Three dimensions (Rank=3):") 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 Console.Write("{0} ", numbers3(i, j, k)) Next k Console.WriteLine() Next j Console.WriteLine() Next i Console.WriteLine("Array.Clear(numbers3, 2, 5)") 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 Console.Write("{0} ", numbers3(i, j, k)) Next k Console.WriteLine() Next j Console.WriteLine() 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 Main() { Console.WriteLine("One dimension (Rank=1):"); int[] numbers1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int i = 0; i < 9; i++) { Console.Write("{0} ", numbers1[i]); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Array.Clear(numbers1, 2, 5)"); Array.Clear(numbers1, 2, 5); for (int i = 0; i < 9; i++) { Console.Write("{0} ", numbers1[i]); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Two dimensions (Rank=2):"); 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++) { Console.Write("{0} ", numbers2[i, j]); } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("Array.Clear(numbers2, 2, 5)"); Array.Clear(numbers2, 2, 5); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Console.Write("{0} ", numbers2[i, j]); } Console.WriteLine(); } Console.WriteLine("Three dimensions (Rank=3):"); 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++) { Console.Write("{0} ", numbers3[i, j, k]); } Console.WriteLine(); } Console.WriteLine(); } Console.WriteLine("Array.Clear(numbers3, 2, 5)"); 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++) { Console.Write("{0} ", numbers3[i, j, k]); } Console.WriteLine(); } Console.WriteLine(); } } } /* 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 */
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Portable Class Library
Supported in: Portable Class LibraryWindows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.