Array.Clear(Array, Int32, Int32)Array.Clear(Array, Int32, Int32)Array.Clear(Array, Int32, Int32) Method

定義

``````public:
static void Clear(Array ^ array, int index, int length);``````
``public static void Clear (Array array, int index, int length);``
``static member Clear : Array * int * int -> unit``

參數

array
Array Array Array

index
Int32 Int32 Int32

length
Int32 Int32 Int32

例外狀況

`array``null``array` is `null`.

`index` 小於 `array` 的下限。`index` is less than the lower bound of `array`.

-或--or- `length` 小於零。`length` is less than zero.

-或--or- `index``length` 的總和大於 `array` 的大小。The sum of `index` and `length` is greater than the size of `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
*/
``````
``````Module Example
Sub Main()
Console.WriteLine(vbLf & "One dimension (Rank=1):")
Dim numbers1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9}

For i As Integer = 0 To 8
Console.Write("{0} ", numbers1(i))
Next
Console.WriteLine()

Console.WriteLine(vbLf & "Array.Clear(numbers1, 2, 5)")
Array.Clear(numbers1, 2, 5)

For i As Integer = 0 To 8
Console.Write("{0} ", numbers1(i))
Next
Console.WriteLine()

Console.WriteLine(vbLf & "Two dimensions (Rank=2):")
Dim numbers2(,) As Integer = {{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }}

For i As Integer = 0 To 2
For j As Integer = 0 To 2
Console.Write("{0} ", numbers2(i, j))
Next
Console.WriteLine()
Next

Console.WriteLine(vbLf & "Array.Clear(numbers2, 2, 5)")
Array.Clear(numbers2, 2, 5)

For i As Integer = 0 To 2
For j As Integer = 0 To 2
Console.Write("{0} ", numbers2(i, j))
Next
Console.WriteLine()
Next

Console.WriteLine(vbLf & "Three dimensions (Rank=3):")
Dim numbers3(,,) As Integer = {{{ 1, 2 }, { 3, 4 }}, _
{{ 5, 6 }, { 7, 8 }}, _
{{ 9, 10 }, { 11, 12 }}}

For i As Integer = 0 To 1
For j As Integer = 0 To 1
For k As Integer = 0 To 1
Console.Write("{0} ", numbers3(i, j, k))
Next
Console.WriteLine()
Next
Console.WriteLine()
Next
Console.WriteLine()

Console.WriteLine("Array.Clear(numbers3, 2, 5)")
Array.Clear(numbers3, 2, 5)

For i As Integer = 0 To 1
For j As Integer = 0 To 1
For k As Integer = 0 To 1
Console.Write("{0} ", numbers3(i, j, k))
Next
Console.WriteLine()
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays 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
``````

``````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
``````
``````Public Structure TimeZoneTime
Private dt As DateTimeOffset
Private tz As TimeZoneInfo

Public Sub New(dateTime As DateTimeOffset, timeZone As TimeZoneInfo)
dt = dateTime
tz = timeZone
End Sub

Public ReadOnly Property DateTime As DateTimeOffset
Get
Return dt
End Get
End Property

Public ReadOnly Property TimeZone As TimeZoneInfo
Get
Return tz
End Get
End Property
End Structure

Module Example
Public Sub Main()
' Declare an array with two elements.
Dim timeZoneTimes() As TimeZoneTime = { New TimeZoneTime(Date.Now, TimeZoneInfo.Local),
New TimeZoneTime(Date.Now, TimeZoneInfo.Utc) }
For Each timeZoneTime In timeZoneTimes
Console.WriteLine("{0}: {1:G}",
If(timeZoneTime.TimeZone Is Nothing, "<null>", timeZoneTime.TimeZone),
timeZoneTime.DateTime)
Next
Console.WriteLine()

Array.Clear(timeZoneTimes, 1, 1)
For Each timeZoneTime In timeZoneTimes
Console.WriteLine("{0}: {1:G}",
If(timeZoneTime.TimeZone Is Nothing, "<null>", timeZoneTime.TimeZone),
timeZoneTime.DateTime)
Next
End Sub
End Module
' The example displays output like the following:
'       (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
``````

備註

Boolean `false`

DateTime DateTime.MinValue