I noticed that arrays have the SetValue method, which seems a little out of place when you could just use the indexers. Is there some special purpose for SetValue? The MSDN article didn't seem to say what SetValue was for, just how to use it. Which method would be more efficient to use as far as speed goes?
SetValue(Object, Object, Object[]) Sets the property value of a specified object with optional index values for index properties.
Assigning values to an element in an array is similar to assigning values to scalar variables. Simply reference an individual element of an array using the array name and the index inside parentheses, then use the assignment operator (=) followed by a value.
If you want to access the nth element without knowing the index, you can use next() n times to reach the nth element.
The index indicates the position of the element within the array (starting from 1) and is either a number or a field containing a number.
Sometimes all you have of an array is that it's an Array
. The Array
class does not have indexers, so the best way to set/get element values on it is via the GetValue
and SetValue
methods. For example:
private void M(Array array)
{
array[0] = 5; // <-- Compiler error
array.SetValue(5, 0); // <-- Works
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With