英語で読む

次の方法で共有


Array.Clear メソッド

定義

オーバーロード

Clear(Array)

配列の内容をクリアします。

Clear(Array, Int32, Int32)

配列内にある要素の範囲を、各要素の型の既定値に設定します。

Clear(Array)

ソース:
Array.CoreCLR.cs
ソース:
Array.CoreCLR.cs
ソース:
Array.CoreCLR.cs

配列の内容をクリアします。

C#
public static void Clear (Array array);

パラメーター

array
Array

消去する配列。

例外

arraynull です。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 6, 7, 8, 9

Clear(Array, Int32, Int32)

ソース:
Array.CoreCLR.cs
ソース:
Array.CoreCLR.cs
ソース:
Array.CoreCLR.cs

配列内にある要素の範囲を、各要素の型の既定値に設定します。

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

パラメーター

array
Array

要素を削除する必要がある配列。

index
Int32

削除する要素の範囲の開始インデックス。

length
Int32

削除する要素の数。

例外

arraynullです。

indexarray の下限を下回っています。

- または -

length が 0 未満です。

- または -

indexlength の合計が array のサイズを超えています。

次の例では、 メソッドを Clear 使用して、1 次元、2 次元、および 3 次元配列の整数値をリセットします。

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

次の例では、フィールドとフィールドを TimeZoneTime 含む TimeZoneInfo 構造体を DateTimeOffset 定義します。 次に、 メソッドを Clear 呼び出して、値の 2 要素配列内の TimeZoneTime 1 つの要素をクリアします。 メソッドは、クリアされた要素の値を オブジェクトの TimeZoneInfo 既定値 () nullと オブジェクトの DateTimeOffset 既定値 () に設定します DateTimeOffset.MinValue

C#
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

注釈

このメソッドは、配列内の各要素を要素型の既定値にリセットします。 参照型の要素 (要素を含む String ) を に null設定し、値型の要素を次の表に示す既定値に設定します。

Type
Boolean false
すべての整数と浮動小数点の数値型 0 (ゼロ)
DateTime DateTime.MinValue
その他の値型 型のフィールドの既定値

クリアされた要素の範囲は、多次元配列内の行間で折り返されます。

このメソッドは、要素の値のみをクリアします。要素自体は削除されません。 配列のサイズは固定です。そのため、要素を追加または削除することはできません。

このメソッドは O(n) 操作です。nlength です。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.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