Array.Clear Metoda

Definicja

Przeciążenia

Clear(Array)

Czyści zawartość tablicy.

Clear(Array, Int32, Int32)

Ustawia zakres elementów w tablicy na wartość domyślną każdego typu elementu.

Clear(Array)

Źródło:
Array.CoreCLR.cs
Źródło:
Array.CoreCLR.cs
Źródło:
Array.CoreCLR.cs

Czyści zawartość tablicy.

C#
public static void Clear (Array array);

Parametry

array
Array

Tablica do wyczyszczenia.

Wyjątki

array to null.

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET 6, 7, 8, 9

Clear(Array, Int32, Int32)

Źródło:
Array.CoreCLR.cs
Źródło:
Array.CoreCLR.cs
Źródło:
Array.CoreCLR.cs

Ustawia zakres elementów w tablicy na wartość domyślną każdego typu elementu.

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

Parametry

array
Array

Tablica, której elementy muszą zostać wyczyszczone.

index
Int32

Początkowy indeks zakresu elementów do wyczyszczenia.

length
Int32

Liczba elementów do wyczyszczenia.

Wyjątki

array to null.

indexwartość jest mniejsza niż dolna granica .array

-lub-

Parametr length ma wartość niższą niż zero.

-lub-

Suma wartości index i length jest większa niż rozmiar .array

Przykłady

W poniższym przykładzie użyto Clear metody do zresetowania wartości całkowitych w jednowymiarowej, dwuwymiarowej i trójwymiarowej tablicy.

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

W poniższym przykładzie zdefiniowano TimeZoneTime strukturę zawierającą TimeZoneInfoDateTimeOffset pole i pole. Następnie wywołuje metodę Clear , aby wyczyścić jeden element w dwuelementowej tablicy TimeZoneTime wartości. Metoda ustawia wartość wyczyszczonego elementu na wartość domyślną TimeZoneInfo obiektu, czyli null, i wartość domyślną DateTimeOffset obiektu, czyli 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

Uwagi

Ta metoda resetuje każdy element w tablicy do wartości domyślnej typu elementu. Ustawia elementy typów odwołań (w tym String elementy) na null, i ustawia elementy typów wartości na wartości domyślne pokazane w poniższej tabeli.

Typ Wartość
Boolean false
Wszystkie typy liczb całkowitych i zmiennoprzecinkowych 0 (zero)
DateTime DateTime.MinValue
Inne typy wartości Wartość domyślna pól typu

Zakres wyczyszczone elementy zawijane z wiersza do wiersza w tablicy wielowymiarowej.

Ta metoda czyści tylko wartości elementów; nie powoduje usunięcia samych elementów. Tablica ma stały rozmiar; dlatego nie można dodawać ani usuwać elementów.

Ta metoda jest operacją O(n), gdzie n to length.

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.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