为什么我不能将indexer作为ref参数传递?

时间:2021-08-19 03:09:05

I understand the rule. But who knows why this is?

我理解规则。但谁知道为什么会这样呢?

If my code is:

如果我的代码是:

List<T> x = new List<T>;
x.Add(new T());
x.Add(new T());
x.Add(new T());

int y = 2;

//And I call a method 
M1(ref x[y]);

ref x[y] is just a pointer to my instance of T of interest isn't it?

ref x [y]只是指向我感兴趣的T实例的指针不是吗?

Why can't I write the line of code call it M1() in the very simple fashion.

为什么我不能以非常简单的方式编写代码行调用它M1()。

.

I know the workaround:

我知道解决方法:

T cX = x[y];
M1(ref cX);

I'm hoping to hear why the architects of c# require this extra step to pass a pointer? Or is it a compiler limitation?

我希望听到为什么c#的架构师需要这个额外的步骤来传递指针?或者它是编译器限制?

2 个解决方案

#1


3  

An indexer value is not classified as a variable; therefore, you cannot pass an indexer value as a ref or out parameter.

索引器值不归类为变量;因此,您不能将索引器值作为ref或out参数传递。

Refer msdn

请参阅msdn

#2


1  

ref x[y] is just a pointer to my instance of T of interest isn't it?

ref x [y]只是指向我感兴趣的T实例的指针不是吗?

Well, if T is a reference type, then yes. If T is a value type then no, but that doesn't matter, let's assume for the moment it is. Since the method is ref T it needs to accept a reference to a variable which is pointer to an instance of T. You just have a pointer, not a storage location for a pointer. (Note that I'm staying within your analogy. Technically you don't have a "pointer", you have a reference to the object, but the analogy is good enough for the purposes of this question.)

好吧,如果T是参考类型,那么是的。如果T是一个值类型,那么不,但这没关系,让我们暂时假设它。由于该方法是ref T,因此需要接受对变量的引用,该变量是指向T实例的指针。您只需要一个指针,而不是指针的存储位置。 (请注意,我保持在你的类比之内。从技术上讲,你没有“指针”,你有一个对象的引用,但这个类比对于这个问题来说已经足够了。)

The getter of the indexer of a list doesn't resolve to a storage location for a given value, it resolves to just a value. Also note that when you write:

列表的索引器的getter不会解析为给定值的存储位置,它只会解析为一个值。还要注意,当你写:

T cX = x[y];
M1(ref cX);

That you're not reflecting any mutations of cX back to the list. You'd need to write:

您没有将cX的任何突变反映到列表中。你需要写:

T cX = x[y];
M1(ref cX);
x[y] = cX;

for it to actually be equivalent to ensure that any mutations to the variable cX in M1 are reflected in the list.

因为它实际上相当于确保M1中变量cX的任何突变都反映在列表中。

#1


3  

An indexer value is not classified as a variable; therefore, you cannot pass an indexer value as a ref or out parameter.

索引器值不归类为变量;因此,您不能将索引器值作为ref或out参数传递。

Refer msdn

请参阅msdn

#2


1  

ref x[y] is just a pointer to my instance of T of interest isn't it?

ref x [y]只是指向我感兴趣的T实例的指针不是吗?

Well, if T is a reference type, then yes. If T is a value type then no, but that doesn't matter, let's assume for the moment it is. Since the method is ref T it needs to accept a reference to a variable which is pointer to an instance of T. You just have a pointer, not a storage location for a pointer. (Note that I'm staying within your analogy. Technically you don't have a "pointer", you have a reference to the object, but the analogy is good enough for the purposes of this question.)

好吧,如果T是参考类型,那么是的。如果T是一个值类型,那么不,但这没关系,让我们暂时假设它。由于该方法是ref T,因此需要接受对变量的引用,该变量是指向T实例的指针。您只需要一个指针,而不是指针的存储位置。 (请注意,我保持在你的类比之内。从技术上讲,你没有“指针”,你有一个对象的引用,但这个类比对于这个问题来说已经足够了。)

The getter of the indexer of a list doesn't resolve to a storage location for a given value, it resolves to just a value. Also note that when you write:

列表的索引器的getter不会解析为给定值的存储位置,它只会解析为一个值。还要注意,当你写:

T cX = x[y];
M1(ref cX);

That you're not reflecting any mutations of cX back to the list. You'd need to write:

您没有将cX的任何突变反映到列表中。你需要写:

T cX = x[y];
M1(ref cX);
x[y] = cX;

for it to actually be equivalent to ensure that any mutations to the variable cX in M1 are reflected in the list.

因为它实际上相当于确保M1中变量cX的任何突变都反映在列表中。