Array.SetValue 方法

定義

將電流 Array 中指定的元素設為指定的值。

多載

名稱 Description
SetValue(Object, Int32)

將該元素設定在一維 Array中指定位置的值。 索引會指定為32位整數。

SetValue(Object, Int32[])

將值設定為多維 Array空間中指定位置的元素。 索引會指定為32位整數的陣列。

SetValue(Object, Int64[])

將值設定為多維 Array空間中指定位置的元素。 索引會指定為64位整數的陣列。

SetValue(Object, Int32, Int32)

將該元素設定在二維 Array中指定位置的值。 索引會指定為32位整數。

SetValue(Object, Int64, Int64)

將該元素設定在二維 Array中指定位置的值。 索引會指定為64位整數。

SetValue(Object, Int32, Int32, Int32)

將值設定為三維 Array中指定位置的元素。 索引會指定為32位整數。

SetValue(Object, Int64, Int64, Int64)

將值設定為三維 Array中指定位置的元素。 索引會指定為64位整數。

SetValue(Object, Int64)

將該元素設定在一維 Array中指定位置的值。 索引會指定為64位整數。

範例

以下程式碼範例示範如何在一維或多維陣列中設定並取得特定值。

using System;

public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a one-dimensional array.
      String[] myArr1 = new String[5];

      // Sets the element at index 3.
      myArr1.SetValue( "three", 3 );
      Console.WriteLine( "[3]:   {0}", myArr1.GetValue( 3 ) );

      // Creates and initializes a two-dimensional array.
      String[,] myArr2 = new String[5,5];

      // Sets the element at index 1,3.
      myArr2.SetValue( "one-three", 1, 3 );
      Console.WriteLine( "[1,3]:   {0}", myArr2.GetValue( 1, 3 ) );

      // Creates and initializes a three-dimensional array.
      String[,,] myArr3 = new String[5,5,5];

      // Sets the element at index 1,2,3.
      myArr3.SetValue( "one-two-three", 1, 2, 3 );
      Console.WriteLine( "[1,2,3]:   {0}", myArr3.GetValue( 1, 2, 3 ) );

      // Creates and initializes a seven-dimensional array.
      String[,,,,,,] myArr7 = new String[5,5,5,5,5,5,5];

      // Sets the element at index 1,2,3,0,1,2,3.
      int[] myIndices = new int[7] { 1, 2, 3, 0, 1, 2, 3 };
      myArr7.SetValue( "one-two-three-zero-one-two-three", myIndices );
      Console.WriteLine( "[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue( myIndices ) );
   }
}


/*
This code produces the following output.

[3]:   three
[1,3]:   one-three
[1,2,3]:   one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

*/
open System

// Creates and initializes a one-dimensional array.
let myArr1 = Array.zeroCreate<string> 5

// Sets the element at index 3.
myArr1.SetValue("three", 3)
printfn $"[3]:   {myArr1.GetValue 3}"

// Creates and initializes a two-dimensional array.
let myArr2 = Array2D.zeroCreate<string> 5 5

// Sets the element at index 1,3.
myArr2.SetValue("one-three", 1, 3)
printfn $"[1,3]:   {myArr2.GetValue(1, 3)}"

// Creates and initializes a three-dimensional array.
let myArr3 = Array3D.zeroCreate<string> 5 5 5

// Sets the element at index 1,2,3.
myArr3.SetValue("one-two-three", 1, 2, 3)
printfn $"[1,2,3]:   {myArr3.GetValue(1, 2, 3)}"

// Creates and initializes a seven-dimensional array.
let myArr7 = Array.CreateInstance(typeof<string>, 5, 5, 5, 5, 5, 5, 5)

// Sets the element at index 1,2,3,0,1,2,3.
let myIndices = [| 1; 2; 3; 0; 1; 2; 3 |]
myArr7.SetValue("one-two-three-zero-one-two-three", myIndices)
printfn $"[1,2,3,0,1,2,3]:   {myArr7.GetValue myIndices}"


// This code produces the following output.
//     [3]:   three
//     [1,3]:   one-three
//     [1,2,3]:   one-two-three
//     [1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three
Public Class SamplesArray

   Public Shared Sub Main()

      ' Creates and initializes a one-dimensional array.
      Dim myArr1(4) As [String]

      ' Sets the element at index 3.
      myArr1.SetValue("three", 3)
      Console.WriteLine("[3]:   {0}", myArr1.GetValue(3))


      ' Creates and initializes a two-dimensional array.
      Dim myArr2(5, 5) As [String]

      ' Sets the element at index 1,3.
      myArr2.SetValue("one-three", 1, 3)
      Console.WriteLine("[1,3]:   {0}", myArr2.GetValue(1, 3))


      ' Creates and initializes a three-dimensional array.
      Dim myArr3(5, 5, 5) As [String]

      ' Sets the element at index 1,2,3.
      myArr3.SetValue("one-two-three", 1, 2, 3)
      Console.WriteLine("[1,2,3]:   {0}", myArr3.GetValue(1, 2, 3))


      ' Creates and initializes a seven-dimensional array.
      Dim myArr7(5, 5, 5, 5, 5, 5, 5) As [String]

      ' Sets the element at index 1,2,3,0,1,2,3.
      Dim myIndices() As Integer = {1, 2, 3, 0, 1, 2, 3}
      myArr7.SetValue("one-two-three-zero-one-two-three", myIndices)
      Console.WriteLine("[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue(myIndices))

   End Sub

End Class


'This code produces the following output.
'
'[3]:   three
'[1,3]:   one-three
'[1,2,3]:   one-two-three
'[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

SetValue(Object, Int32)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將該元素設定在一維 Array中指定位置的值。 索引會指定為32位整數。

public:
 void SetValue(System::Object ^ value, int index);
public void SetValue(object value, int index);
public void SetValue(object? value, int index);
member this.SetValue : obj * int -> unit
Public Sub SetValue (value As Object, index As Integer)

參數

value
Object

指定元素的新值。

index
Int32

一個 32 位元整數,代表要設定的元素位置 Array

例外狀況

電流 Array 並非完全單一維度。

value 無法轉換為電流 Array的元素類型。

index 超出當前 Array有效指標範圍。

備註

GetLowerBoundGetUpperBound方法可判定 的index值是否超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於

SetValue(Object, Int32[])

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將值設定為多維 Array空間中指定位置的元素。 索引會指定為32位整數的陣列。

public:
 void SetValue(System::Object ^ value, ... cli::array <int> ^ indices);
public void SetValue(object value, params int[] indices);
public void SetValue(object? value, params int[] indices);
member this.SetValue : obj * int[] -> unit
Public Sub SetValue (value As Object, ParamArray indices As Integer())

參數

value
Object

指定元素的新值。

indices
Int32[]

一個一維的 32 位元整數陣列,代表指定要設定元素位置的索引。

例外狀況

indicesnull

電流 Array 中的維度數不等於 中的 indices元素數。

value 無法轉換為電流 Array的元素類型。

任何元素 indices 都不在當前 Array維度有效指標範圍內。

備註

元素 indices 數必須等於 中的 Array維度數。 陣列中 indices 的所有元素必須共同指定該元素在多維 Array空間中的位置。

GetLowerBoundGetUpperBound方法可判斷陣列中是否有任何值indices超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於

SetValue(Object, Int64[])

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將值設定為多維 Array空間中指定位置的元素。 索引會指定為64位整數的陣列。

public:
 void SetValue(System::Object ^ value, ... cli::array <long> ^ indices);
public void SetValue(object? value, params long[] indices);
public void SetValue(object value, params long[] indices);
[System.Runtime.InteropServices.ComVisible(false)]
public void SetValue(object value, params long[] indices);
member this.SetValue : obj * int64[] -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.SetValue : obj * int64[] -> unit
Public Sub SetValue (value As Object, ParamArray indices As Long())

參數

value
Object

指定元素的新值。

indices
Int64[]

一個一維的 64 位元整數陣列,代表指定要設定元素位置的索引。

屬性

例外狀況

indicesnull

電流 Array 中的維度數不等於 中的 indices元素數。

value 無法轉換為電流 Array的元素類型。

任何元素 indices 都不在當前 Array維度有效指標範圍內。

備註

元素 indices 數必須等於 中的 Array維度數。 陣列中 indices 的所有元素必須共同指定該元素在多維 Array空間中的位置。

GetLowerBoundGetUpperBound方法可判斷陣列中是否有任何值indices超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於

SetValue(Object, Int32, Int32)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將該元素設定在二維 Array中指定位置的值。 索引會指定為32位整數。

public:
 void SetValue(System::Object ^ value, int index1, int index2);
public void SetValue(object? value, int index1, int index2);
public void SetValue(object value, int index1, int index2);
member this.SetValue : obj * int * int -> unit
Public Sub SetValue (value As Object, index1 As Integer, index2 As Integer)

參數

value
Object

指定元素的新值。

index1
Int32

一個 32 位元整數,代表要設定元素的第一 Array 維索引。

index2
Int32

一個 32 位元整數,代表要設定元素 Array 的二維索引。

例外狀況

電流 Array 並非完全有二維。

value 無法轉換為電流 Array的元素類型。

index1index2 或 都超出當前維 Array度 的有效指標範圍。

備註

GetLowerBoundGetUpperBound 方法可以判斷是否有任何索引超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於

SetValue(Object, Int64, Int64)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將該元素設定在二維 Array中指定位置的值。 索引會指定為64位整數。

public:
 void SetValue(System::Object ^ value, long index1, long index2);
public void SetValue(object? value, long index1, long index2);
public void SetValue(object value, long index1, long index2);
[System.Runtime.InteropServices.ComVisible(false)]
public void SetValue(object value, long index1, long index2);
member this.SetValue : obj * int64 * int64 -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.SetValue : obj * int64 * int64 -> unit
Public Sub SetValue (value As Object, index1 As Long, index2 As Long)

參數

value
Object

指定元素的新值。

index1
Int64

一個 64 位元整數,代表要設定元素的第一 Array 維索引。

index2
Int64

一個 64 位元整數,代表要設定元素的 Array 二維索引。

屬性

例外狀況

電流 Array 並非完全有二維。

value 無法轉換為電流 Array的元素類型。

index1index2 或 都超出當前維 Array度 的有效指標範圍。

備註

GetLowerBoundGetUpperBound 方法可以判斷是否有任何索引超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於

SetValue(Object, Int32, Int32, Int32)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將值設定為三維 Array中指定位置的元素。 索引會指定為32位整數。

public:
 void SetValue(System::Object ^ value, int index1, int index2, int index3);
public void SetValue(object? value, int index1, int index2, int index3);
public void SetValue(object value, int index1, int index2, int index3);
member this.SetValue : obj * int * int * int -> unit
Public Sub SetValue (value As Object, index1 As Integer, index2 As Integer, index3 As Integer)

參數

value
Object

指定元素的新值。

index1
Int32

一個 32 位元整數,代表要設定元素的第一 Array 維索引。

index2
Int32

一個 32 位元整數,代表要設定元素 Array 的二維索引。

index3
Int32

一個 32 位元整數,代表要設定元素的 Array 第三維索引。

例外狀況

電流 Array 並非完全三維。

value 無法轉換為電流 Array的元素類型。

index1index2index3 不在當前維度 Array的有效指標範圍內。

備註

GetLowerBoundGetUpperBound 方法可以判斷是否有任何索引超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於

SetValue(Object, Int64, Int64, Int64)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將值設定為三維 Array中指定位置的元素。 索引會指定為64位整數。

public:
 void SetValue(System::Object ^ value, long index1, long index2, long index3);
public void SetValue(object? value, long index1, long index2, long index3);
public void SetValue(object value, long index1, long index2, long index3);
[System.Runtime.InteropServices.ComVisible(false)]
public void SetValue(object value, long index1, long index2, long index3);
member this.SetValue : obj * int64 * int64 * int64 -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.SetValue : obj * int64 * int64 * int64 -> unit
Public Sub SetValue (value As Object, index1 As Long, index2 As Long, index3 As Long)

參數

value
Object

指定元素的新值。

index1
Int64

一個 64 位元整數,代表要設定元素的第一 Array 維索引。

index2
Int64

一個 64 位元整數,代表要設定元素的 Array 二維索引。

index3
Int64

一個 64 位元整數,代表要設定元素的 Array 第三維索引。

屬性

例外狀況

電流 Array 並非完全三維。

value 無法轉換為電流 Array的元素類型。

index1index2index3 不在當前維度 Array的有效指標範圍內。

備註

GetLowerBoundGetUpperBound 方法可以判斷是否有任何索引超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於

SetValue(Object, Int64)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將該元素設定在一維 Array中指定位置的值。 索引會指定為64位整數。

public:
 void SetValue(System::Object ^ value, long index);
public void SetValue(object? value, long index);
public void SetValue(object value, long index);
[System.Runtime.InteropServices.ComVisible(false)]
public void SetValue(object value, long index);
member this.SetValue : obj * int64 -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.SetValue : obj * int64 -> unit
Public Sub SetValue (value As Object, index As Long)

參數

value
Object

指定元素的新值。

index
Int64

一個 64 位元整數,代表要設定的元素位置 Array

屬性

例外狀況

電流 Array 並非完全單一維度。

value 無法轉換為電流 Array的元素類型。

index 超出當前 Array有效指標範圍。

備註

GetLowerBoundGetUpperBound方法可判定 的index值是否超出範圍。

欲了解更多關於轉換的資訊,請參見 Convert

此方法是 O(1) 作業。

Note

SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。

另請參閱

適用於