为什么我们不能将索引器产生的值作为ref或out参数传递给方法? [英] why we can't pass produced value by indexer as ref or out parameter to an method?
本文介绍了为什么我们不能将索引器产生的值作为ref或out参数传递给方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用索引器有两个重要限制.首先,因为索引器会执行
未定义存储位置,索引器产生的值不能作为ref或
传递
方法的out参数.其次,索引器必须是其类的实例成员.它
无法声明为静态.
1.为什么我们不能将索引器产生的值作为ref或out参数传递给方法?
2.为什么索引器不能是静态的?
There are two important restrictions to using indexers. First,because an indexer does
not define a storage location, a value produced by an indexer cannot be passed as a ref or
out parameter to a method. Second, an indexer must be an instance member of its class; it
cannot be declared static.
1. why we can''t pass produced value by indexer as ref or out parameter to an method?
2. why indexers can not be static?
推荐答案
您的引号非常准确地回答了您的第一个问题:
1)因为索引器未定义存储位置"
它不会产生任何与存储相关的值,因此您无法在其上添加引用.如果没有引用可以指向的值,则它不能是引用,因此不能作为引用传递.索引器不是数字:它是处于执行阶段的过程.
2)因为它是该类特定实例的索引器-如果它是静态的,它将是所有实例的全局索引器,这将是愚蠢的-如果您有两个列表,则全局索引器应返回其中的哪一个价值从何而来?如果它们具有不同数量的元素怎么办?索引器必须与特定的可索引实例相关,否则它们将无法工作!
Your quote answers your first question pretty exactly:
1) "because an indexer does not define a storage location"
It doesn''t produce any storage related value so you can''t pint a reference at it. If it doesn''t have a value that a reference can point at, it can''t be a reference, so it can''t be passed as a reference. An Indexer is not a number: it is a process in a stage of execution.
2) Because it is an indexer into a specific instance of the class - if it was static, it would be a global indexer for all instances, and that would be silly - if you have two lists which of them should a global indexer return a value from? What if they have a different number of elements? Indexers have to be related to the specific indexable instance, or they can''t work!
这篇关于为什么我们不能将索引器产生的值作为ref或out参数传递给方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文