Edit

Share via


Array.Clear Method

Definition

Overloads

Clear(Array)

Clears the contents of an array.

Clear(Array, Int32, Int32)

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

Clear(Array)

Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs

Clears the contents of an array.

C#
public static void Clear(Array array);

Parameters

array
Array

The array to clear.

Exceptions

array is null.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Clear(Array, Int32, Int32)

Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs

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

C#
public static void Clear(Array array, int index, int length);

Parameters

array
Array

The array whose elements need to be cleared.

index
Int32

The starting index of the range of elements to clear.

length
Int32

The number of elements to clear.

Exceptions

array is null.

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.

Examples

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

C#
using System;

class Example
{
    public static void RunIt()
    {
        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("\nThree dimensions (Rank=3):");
        int[,,] numbers3 = {{{1, 2}, {3, 4}},
                             {{5, 6}, {7, 8}},
                             {{9, 10}, {11, 12}}};

        for (int i = 0; i < 3; 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 < 3; 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
 * 
 * 9 10
 * 11 12
 * 
 * Array.Clear(numbers3, 2, 5)
 * 1 2
 * 0 0
 * 
 * 0 0
 * 0 8
 * 
 * 9 10
 * 11 12
 */

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.

C#
using System;

public struct TimeZoneTime
{
    public TimeZoneTime(DateTimeOffset dateTime, TimeZoneInfo timeZone)
    {
        DateTime = dateTime;
        TimeZone = timeZone;
    }

    public DateTimeOffset DateTime { get; }

    public TimeZoneInfo TimeZone { get; }
}

public class Example1
{
    public static void RunIt()
    {
        // Declare an array with two elements.
        TimeZoneTime[] timeZoneTimes = {
            new(DateTime.Now, TimeZoneInfo.Local),
            new(DateTime.Now, TimeZoneInfo.Utc)
        };
        foreach (var timeZoneTime in timeZoneTimes)
            Console.WriteLine("{0}: {1:G}",
                              timeZoneTime.TimeZone == null ? "<null>" : timeZoneTime.TimeZone.ToString(),
                              timeZoneTime.DateTime);
        Console.WriteLine();

        Array.Clear(timeZoneTimes, 1, 1);
        foreach (var timeZoneTime in timeZoneTimes)
            Console.WriteLine("{0}: {1:G}",
                              timeZoneTime.TimeZone == null ? "<null>" : timeZoneTime.TimeZone.ToString(),
                              timeZoneTime.DateTime);
    }
}

// The example displays the following output:
//       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
//       UTC: 1/20/2014 12:11:00 PM
//
//       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
//       <null>: 1/1/0001 12:00:00 AM

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.

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 can't be added or removed.

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0