System.Array.Clear Method

Sets a range of elements in an array to the default value of each element type.

Syntax

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.Success)]
public static void Clear (Array array, int index, int length)

Parameters

array
The array whose elements need to be cleared.
index
The starting index of the range of elements to clear.
length
The number of elements to clear.

Exceptions

TypeReason
ArgumentNullException array is null.
ArgumentOutOfRangeException

index < array.GetLowerBound(0).

length < 0.

index and length do not specify a valid range in array (i.e. index + length > array.GetLowerBound(0) + array.Length ).

Remarks

This method resets each element in an array to the element type's default value. It sets elements of reference types (including string elements) to null, and sets elements of value types to the default values shown in the following table.

bool

false

All integral and floating point numeric types

0 (zero)

DateTime

DateTime.MinValue

Other value types

Default value of the type's fields

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.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0