Array.SetValue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將電流 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
指定元素的新值。
例外狀況
電流 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 位元整數陣列,代表指定要設定元素位置的索引。
例外狀況
indices 是 null。
電流 Array 中的維度數不等於 中的 indices元素數。
value 無法轉換為電流 Array的元素類型。
任何元素 indices 都不在當前 Array維度有效指標範圍內。
備註
元素 indices 數必須等於 中的 Array維度數。 陣列中 indices 的所有元素必須共同指定該元素在多維 Array空間中的位置。
GetLowerBound與GetUpperBound方法可判斷陣列中是否有任何值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 位元整數陣列,代表指定要設定元素位置的索引。
- 屬性
例外狀況
indices 是 null。
電流 Array 中的維度數不等於 中的 indices元素數。
value 無法轉換為電流 Array的元素類型。
任何元素 indices 都不在當前 Array維度有效指標範圍內。
備註
元素 indices 數必須等於 中的 Array維度數。 陣列中 indices 的所有元素必須共同指定該元素在多維 Array空間中的位置。
GetLowerBound與GetUpperBound方法可判斷陣列中是否有任何值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
指定元素的新值。
例外狀況
電流 Array 並非完全有二維。
value 無法轉換為電流 Array的元素類型。
或 index1index2 或 都超出當前維 Array度 的有效指標範圍。
備註
GetLowerBound和 GetUpperBound 方法可以判斷是否有任何索引超出範圍。
欲了解更多關於轉換的資訊,請參見 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
指定元素的新值。
- 屬性
例外狀況
電流 Array 並非完全有二維。
value 無法轉換為電流 Array的元素類型。
或 index1index2 或 都超出當前維 Array度 的有效指標範圍。
備註
GetLowerBound和 GetUpperBound 方法可以判斷是否有任何索引超出範圍。
欲了解更多關於轉換的資訊,請參見 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
指定元素的新值。
例外狀況
電流 Array 並非完全三維。
value 無法轉換為電流 Array的元素類型。
index1 或 index2 或 index3 不在當前維度 Array的有效指標範圍內。
備註
GetLowerBound和 GetUpperBound 方法可以判斷是否有任何索引超出範圍。
欲了解更多關於轉換的資訊,請參見 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
指定元素的新值。
- 屬性
例外狀況
電流 Array 並非完全三維。
value 無法轉換為電流 Array的元素類型。
index1 或 index2 或 index3 不在當前維度 Array的有效指標範圍內。
備註
GetLowerBound和 GetUpperBound 方法可以判斷是否有任何索引超出範圍。
欲了解更多關於轉換的資訊,請參見 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
指定元素的新值。
- 屬性
例外狀況
電流 Array 並非完全單一維度。
value 無法轉換為電流 Array的元素類型。
index 超出當前 Array有效指標範圍。
備註
與GetLowerBoundGetUpperBound方法可判定 的index值是否超出範圍。
欲了解更多關於轉換的資訊,請參見 Convert。
此方法是 O(1) 作業。
Note
若 SetValue 用於指派 null 值型別陣列中的元素,該元素的所有欄位初始化為零。 該元素的值並非空參考,且無法透過搜尋空參考找到。