本文為機器翻譯文章。如需檢視英文版,請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上,即可在快顯視窗顯示英文原文。
譯文
原文

Array.Clear 方法 (Array, Int32, Int32)

 

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

命名空間:   System
組件:  mscorlib (在 mscorlib.dll 中)

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

參數

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 T:System.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 M:System.Array.Clear(System.Array,System.Int32,System.Int32) 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 T:System.TimeZoneInfo field and a T:System.DateTimeOffset field. It then calls the M:System.Array.Clear(System.Array,System.Int32,System.Int32) 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 T:System.TimeZoneInfo object, which is null, and the default value of a T:System.DateTimeOffset object, which is F:System.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

Universal Windows Platform
自 4.5 起可用
.NET Framework
自 1.1 起可用
Portable Class Library
支援版本:portable .NET platforms
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用
Windows Phone
自 8.1 起可用
回到頁首
顯示: