使用数组的SetValue方法与[]索引器 [英] Using an array's SetValue method vs. the [] indexers

查看:184
本文介绍了使用数组的SetValue方法与[]索引器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到数组具有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类没有索引器,因此在其上设置/获取元素值的最佳方法是通过GetValueSetValue方法.例如:

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方法与[]索引器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆