问题描述
我注意到数组具有SetValue方法,当您仅可以使用索引器时,这似乎有点不合适. SetValue是否有一些特殊用途? MSDN文章似乎并没有说明SetValue的用途,只是说如何使用它.就速度而言,哪种方法更有效?
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?
推荐答案
有时候,您拥有的一个数组就是它是一个Array
. Array
类没有索引器,因此在其上设置/获取元素值的最佳方法是通过GetValue
和SetValue
方法.例如:
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
}
这篇关于使用数组的SetValue方法与[]索引器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!