【发布时间】:2016-11-04 00:21:21
【问题描述】:
例如,我知道List<T> 索引器类似于属性。取自msdn:
索引器允许类或结构的实例被索引就像 数组。索引器类似于属性,只是它们的访问器采用 参数。
但我不明白为什么会发生以下情况:
int[] myArray = new int[0];
List<int> myList = new List<int>();
Interlocked.Increment(ref myArray[0]); // fine
Interlocked.Increment(ref myList[0]); //CS0206 A property or indexer may not be passed as an out or ref parameter
他们不应该以同样的方式工作吗?
【问题讨论】:
-
请检查 Mike Chamberlain 对重复问题的回答。尽管它没有被标记为正确答案是更详细的答案,但它确实清楚地解释了为什么这是不可能的。希望对您有所帮助。
-
@Christos Imo 它没有回答问题
-
@Christos 我不是在问这个错误。
-
@Christos 我同意 Zbigniew。真正的问题是为什么它适用于数组,而不是为什么它不适用于列表索引器。
-
因为数组是特殊的。