c# - 使用数组的 SetValue 方法与 [] 索引器

标签 c# arrays indexer setvalue

我注意到数组有 SetValue 方法,当您只能使用索引器时,这似乎有点不合适。 SetValue 有什么特殊用途吗? MSDN 文章似乎并没有说明 SetValue 的用途,只是说明了如何使用它。就速度而言,使用哪种方法效率更高?

最佳答案

有时你所拥有的数组只是一个数组Array 类没有索引器,因此在其上设置/获取元素值的最佳方法是通过 GetValueSetValue 方法。例如:

private void M(Array array) 
{
    array[0] = 5;         // <-- Compiler error
    array.SetValue(5, 0); // <-- Works
}

关于c# - 使用数组的 SetValue 方法与 [] 索引器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626892/

相关文章:

Javascript:在数组中搜索重复项

c - C 中整数数组的 shell 排序

c - 大小为 8 的无效写入,C Valgrind,字符串数组

android - 如何在 ListView 的特定位置添加分隔符?

c# - 在 visual studio 中调试循环时如何知道哪一行出错?

c# - 如何根据属性合并两个列表?

c# - 使用 Microsoft.Web.Administration 以编程方式安装 SSL 证书

C#:如何控制组合框中显示的当前项目索引?

python - 尝试使用 .loc[row_indexer,col_indexer] = value 代替

ref - 带有 Ref Return 的 C# 索引器也支持集合