Export (0) Print
Expand All

Array.Clear Method

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

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

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

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.

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

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

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.

using System;

public struct TimeZoneTime
{
   private DateTimeOffset dt;
   private TimeZoneInfo tz;

   public TimeZoneTime(DateTimeOffset dateTime, TimeZoneInfo timeZone)
   {
      dt = dateTime;
      tz = timeZone;
   }

   public DateTimeOffset DateTime 
   { get { return dt; } }

   public TimeZoneInfo TimeZone 
   { get { return tz; } }
}

public class Example
{
   public static void Main()
   {
      // Declare an array with two elements.
      TimeZoneTime[] timeZoneTimes = { new TimeZoneTime(DateTime.Now, TimeZoneInfo.Local),
                                       new TimeZoneTime(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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft