参照C#参考? [英] Reference to reference in C#?

查看:92
本文介绍了参照C#参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家都知道,C#类对象被当作参考,所以当你传递一个参考对象作为一个方法的引用会发生什么?说我们有:

As we all know, C# classes object are treated as references, so what happens when you pass a reference object as a reference to a method? Say we have:

public class A { ... }

和则:

public void F(ref A a) { ... }

该编译器发现, A 已经是一个引用类型,并保持这种方式,或者他创建了一个新的引用该对象

Does the compiler find out that a is already a reference type and keep it that way, or he creates a new reference to that object?

如果我们有这样的事情是什么:<? / p>

And what if we have something like this:

public void F(ref A a)
{
    F(ref a);
}

在这段代码中,除了明显的 StackOverflowException ,请问编译器创建引用来引用...要参照 A 这是一个参考对象?

In this code, besides the obvious StackOverflowException, does the compiler creates reference to reference ... to reference to a which is a reference object?

推荐答案

这是最好的一个例子来说明:

This is best illustrated with an example:

public class C { public int P { get; set; } }
public class X
{
    static void M(C c1, C c2, ref C c3, ref C c4)
    {
      c1.P = 11;
      c2 = new C() { P = 12 };
      c3.P = 13;
      c4 = new C() { P = 14 };
    }
    static void Main()
    {
      C q1 = new C() { P = 1 };
      C q2 = new C() { P = 2 };
      C q3 = new C() { P = 3 };
      C q4 = new C() { P = 4 };
      M(q1, q2, ref q3, ref q4);
      Console.WriteLine(q1.P);
      Console.WriteLine(q2.P);
      Console.WriteLine(q3.P);
      Console.WriteLine(q4.P);
    }
}



会发生什么?

What happens?

q1和C1指的是相同的对象,但是是不同的变量。不同诱变c1.P变异q1.P因为两个变量指的是同一个对象,所以现在q1表示11

q1 and c1 refer to the same object but are different variables. Mutating c1.P mutates q1.P because both variables refer to the same object, so q1 is now 11.

Q 2和C2指代相同的对象,但是是不同的变量。变异C2不发生变异Q2因为C2和Q2是不同的变量;改变一个不会改变另一个。 Q2保持2,并且新的对象被丢失。

q2 and c2 refer to the same object but are different variables. Mutating c2 does not mutate q2 because c2 and q2 are different variables; changing one does not change the other. q2 stays 2, and the new object is lost.

Q3和c3是对相同的变量两个名字,因此指的是相同的对象。当您更改c3.P改变q3.P自动,因为他们是两个名称相同的事情。

q3 and c3 are two names for the same variable, and therefore refer to the same object. When you change c3.P that changes q3.P automatically because they are two names for the same thing.

Q4和C4两个名字相同的变量,因此,变异Q4也发生变异C4。

q4 and c4 are two names for the same variable, and therefore mutating q4 also mutates c4.

这是否有道理?

不幸的是关键字使别名这个变量,是参考。 。这本来是更清楚它当时的别名

It is unfortunate that the keyword for "make an alias to this variable" is "ref". It would have been more clear had it been "alias".

要回答你的第二个问题:不,这不会使引用链。让我们更加清晰的例子:

To answer your second question: no, this does not make a chain of references. Let's make a more clear example:

...
int c1 = 123;
M(ref c1);
...
void M1(ref int q1) { M2(ref q1); }
void M2(ref int q2) { M2(ref q2); }

这表示,c1和Q1是相同的变量不同的名称,和q1和q2是不同对相同的变量,因此C1,q1和q2名称是为彼此所有别名。从未有一个引用来引用变量,在C#中有C ++的方式。

This says that c1 and q1 are different names for the same variable, and q1 and q2 are different names for the same variable, and therefore c1, q1 and q2 are all aliases for each other. There's never a "reference to reference to variable" in C# the way there is in C++.

这篇关于参照C#参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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