Array.Clear Method (Array, Int32, Int32)

 

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

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

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 array.

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.

Type

Value

Boolean

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.

The following example uses the Clear method to reset integer values in a one-dimensional, two-dimensional, and three-dimensional array.

No code example is currently available or this language may not be supported.

The following example defines a TimeZoneTime structure that includes a TimeZoneInfo field and a DateTimeOffset field. It then calls the Clear method to clear one element in a two-element array of TimeZoneTime values. The method sets the value of the cleared element to the default value of a TimeZoneInfo object, which is null, and the default value of a DateTimeOffset object, which is DateTimeOffset.MinValue.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: