System.Array.SetValue Method

Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.

Syntax

public void SetValue (object value, int index)

Parameters

value
The new value for the specified element.
index
A 32-bit integer that represents the position of the Array element to set.

Exceptions

TypeReason
ArgumentExceptionThe current instance has more than one dimension.
IndexOutOfRangeException index is outside the range of valid indices for the current instance.
InvalidCastException value is not assignment-compatible with the element type of the current instance.

Remarks

The Array.GetLowerBound(int) and Array.GetUpperBound(int) methods can determine whether the value of index is out of bounds.

For more information about conversions, see Convert.

This method is an O(1) operation.

Note:

If erload:System.Array.SetValue is used to assign null to an element of an array of value types, all fields of the element are initialized to zero. The value of the element is not a null reference, and cannot be found by searching for a null reference.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0