Array.SetValue 方法 (System)

Array.SetValue 方法 (System)

Array.SetValue 方法

参考

反馈

定义

命名空间:

System

程序集:System.Runtime.dll

程序集:mscorlib.dll

程序集:netstandard.dll

重要

一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。

将当前 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 namespace System;

int main()

{

// Creates and initializes a one-dimensional array.

array^myArr1 = gcnew array(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.

array^myArr2 = gcnew array(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.

array^myArr3 = gcnew array(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.

array^myArr7 = gcnew array(5,5,5,5,5,5,5);

// Sets the element at index 1,2,3,0,1,2,3.

array^myIndices = {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

*/

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 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 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 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, 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

将值设置为一维 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 元素的位置。

例外

ArgumentException

当前 Array 不是正好具有一个维度。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException

index 超出了当前 Array 的有效索引的范围。

注解

GetLowerBound和 GetUpperBound 方法可以确定 的值index是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

SetValue(Object, Int32[])

Source:Array.cs

Source:Array.cs

Source:Array.cs

将值设置为多维 Array 中指定位置的元素。 索引指定为一个 32 位整数数组。

public:

void SetValue(System::Object ^ value, ... cli::array ^ 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 位整数的一维数组,它表示用于指定要设置的元素的位置的索引。

例外

ArgumentNullException

indices 为 null。

ArgumentException

当前 Array 中的维数不等于 indices 中的元素数。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException

indices 中的任何元素都超出了当前 Array 的相应维度的有效索引范围。

注解

中的 indices 元素数必须等于 中的 Array维度数。 数组中的所有 indices 元素必须共同指定所需元素在多维 Array中的位置。

GetLowerBound和 GetUpperBound 方法可以确定数组中的任何indices值是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

SetValue(Object, Int64[])

Source:Array.cs

Source:Array.cs

Source:Array.cs

将值设置为多维 Array 中指定位置的元素。 索引指定为一个 64 位整数数组。

public:

void SetValue(System::Object ^ value, ... cli::array ^ 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

[]

member this.SetValue : obj * int64[] -> unit

Public Sub SetValue (value As Object, ParamArray indices As Long())

参数

value

Object

指定元素的新值。

indices

Int64[]

64 位整数的一维数组,它表示用于指定要设置元素的位置索引。

属性

ComVisibleAttribute

例外

ArgumentNullException

indices 为 null。

ArgumentException

当前 Array 中的维数不等于 indices 中的元素数。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

ArgumentOutOfRangeException

indices 中的任何元素都超出了当前 Array 的相应维度的有效索引范围。

注解

中的 indices 元素数必须等于 中的 Array维度数。 数组中的所有 indices 元素必须共同指定所需元素在多维 Array中的位置。

GetLowerBound和 GetUpperBound 方法可以确定数组中的任何indices值是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

SetValue(Object, Int32, Int32)

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 元素的第二维索引。

例外

ArgumentException

当前 Array 不是正好具有两个维度。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException

index1 或 index2 超出了当前 Array 的相应维度的有效索引范围。

注解

GetLowerBound和 GetUpperBound 方法可以确定任何索引是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

SetValue(Object, Int64, Int64)

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

[]

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 元素的第二维索引。

属性

ComVisibleAttribute

例外

ArgumentException

当前 Array 不是正好具有两个维度。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

ArgumentOutOfRangeException

index1 或 index2 超出了当前 Array 的相应维度的有效索引范围。

注解

GetLowerBound和 GetUpperBound 方法可以确定任何索引是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

SetValue(Object, Int32, Int32, Int32)

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 元素的第三维索引。

例外

ArgumentException

当前 Array 不是正好具有三个维度。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException

index1 或 index2 或 index3 超出了当前 Array 的相应维度的有效索引范围。

注解

GetLowerBound和 GetUpperBound 方法可以确定任何索引是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

SetValue(Object, Int64, Int64, Int64)

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

[]

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 元素的第三维索引。

属性

ComVisibleAttribute

例外

ArgumentException

当前 Array 不是正好具有三个维度。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

ArgumentOutOfRangeException

index1 或 index2 或 index3 超出了当前 Array 的相应维度的有效索引范围。

注解

GetLowerBound和 GetUpperBound 方法可以确定任何索引是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

SetValue(Object, Int64)

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

[]

member this.SetValue : obj * int64 -> unit

Public Sub SetValue (value As Object, index As Long)

参数

value

Object

指定元素的新值。

index

Int64

一个 64 位整数,它表示要设置的 Array 元素的位置。

属性

ComVisibleAttribute

例外

ArgumentException

当前 Array 不是正好具有一个维度。

InvalidCastException

value 不能转换为当前 Array 的元素类型。

ArgumentOutOfRangeException

index 超出了当前 Array 的有效索引的范围。

注解

GetLowerBound和 GetUpperBound 方法可以确定 的值index是否超出边界。

有关转换的详细信息,请参阅 Convert。

此方法是 O (1) 操作。

注意

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

另请参阅

GetLowerBound(Int32)

GetUpperBound(Int32)

GetValue

适用于

相关推荐

Visio流程图绘制教程:一看就会的6个关键步骤详解
365bet官网开户

Visio流程图绘制教程:一看就会的6个关键步骤详解

📅 02-09 👁️ 6218
详解python 利用 pyecharts 画地图(热力图)(世界地图,省市地图,区县地图)、动态流向图
东软集团交流讨论
bt365全程担保

东软集团交流讨论

📅 08-16 👁️ 2911
GBC充电电池,求推荐
bt365全程担保

GBC充电电池,求推荐

📅 08-29 👁️ 6198
亡秦必楚,秦国是如何灭楚国的?
365bet体坛即时比分

亡秦必楚,秦国是如何灭楚国的?

📅 09-15 👁️ 3493
信息经济学
365bet体坛即时比分

信息经济学

📅 02-05 👁️ 7217