Array.SetValue 方法

定义

将当前 Array 中的指定元素设置为指定值。

重载

名称 说明
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)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于

SetValue(Object, Int32[])

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于

SetValue(Object, Int64[])

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于

SetValue(Object, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于

SetValue(Object, Int64, Int64)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于

SetValue(Object, Int32, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于

SetValue(Object, Int64, Int64, Int64)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于

SetValue(Object, Int64)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
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) 操作。

注释

如果 SetValue 用于分配给 null 值类型的数组的元素,则元素的所有字段将初始化为零。 元素的值不是 null 引用,并且无法通过搜索 null 引用找到。

另请参阅

适用于