[英]How is “this” passed in C#
I'm trying to understand how "this" gets passed around as a property in C#-6.0 (VS 2015). 我试图理解“这个”如何作为C#-6.0(VS 2015)中的属性传递。
using System;
public class Person
{
private Person instance;
public Person()
{
instance = this;
}
public Person myself
{
get { return instance; }
set { instance = value; }
}
public string name = "Eddie";
}
public class Example
{
public static void Main()
{
Person firstPerson = new Person();
Person secondPerson = firstPerson.myself;
secondPerson.name = "Bill";
Console.WriteLine(firstPerson.name);
Console.WriteLine(secondPerson.name);
firstPerson.myself = new Person();
Console.WriteLine(firstPerson.name);
Console.WriteLine(secondPerson.name);
Console.ReadLine();
}
}
My assumption is that when the line: 我的假设是当线:
Person secondPerson = firstPerson.myself;
is run, that secondPerson becomes a reference to firstPerson, so when I change the name to "Bill", firstPerson.name
and secondPerson.name
are both Bill. 运行时,secondPerson成为firstPerson的引用,所以当我将名称更改为“Bill”时, firstPerson.name
和secondPerson.name
都是Bill。 But when I run 但是当我跑步的时候
firstPerson.myself = new Person();
I expected firstPerson.name
and secondPerson.name
to go back to "Eddie", but it remains "Bill". 我期望firstPerson.name
和secondPerson.name
回到“Eddie”,但它仍然是“Bill”。 Why? 为什么? Thanks in advance! 提前致谢!
You've changed the Person
instance that firstPerson.instance
is pointing to, but not the original instance that firstPerson
refers to. 您已经更改了firstPerson.instance
指向的Person
实例,但没有firstPerson
引用的原始实例。
So firstPerson
is still pointing to the original Person
instance (and so firstPerson.name
returns the value set in the first instance), while firstPerson.instance
is now pointing to a new (second) Person
instance. 因此, firstPerson
仍指向原始Person
实例(因此firstPerson.name
返回第一个实例中设置的值),而firstPerson.instance
现在指向新的(第二个) Person
实例。
Person firstPerson = new Person(); // instance 1
Person secondPerson = firstPerson.myself; // myself refers to instance 1
secondPerson.name = "Bill"; // set name in instance 1
Console.WriteLine(firstPerson.name); // get name from instance 1
Console.WriteLine(secondPerson.name); // get name from myself in instance 1
Console.WriteLine(firstPerson.myself.name); // get name from instance 1 (same as above)
firstPerson.myself = new Person(); // myself refers to instance 2, but firstPerson still refers to instance 1
Console.WriteLine(firstPerson.name); // still getting name from instance 1
Console.WriteLine(secondPerson.name); // still getting name from myself in instance 1
Console.WriteLine(firstPerson.myself.name); // get name from instance 2 (since firstPerson.myself was reassigned)
firstPerson = new Person(); // firstPerson and firstPerson.myself point to instance 3
Console.WriteLine(firstPerson.name); // get name from instance 3, which is the default "Eddie"
Console.WriteLine(secondPerson.name); // still points to instance 1, since that's what it was when it was assigned
Console.WriteLine(firstPerson.myself.name); // get name from instance 3 (since firstPerson.myself is defaults to the new instance again)
this
represent current instance of of a class. this
表示类的当前实例。
When you are creating new instance of Person firstPerson.mySelf
, that time it will refer to the new instance of Person
class. 当您创建Person firstPerson.mySelf
新实例时,它将引用Person
类的新实例。
Person firstPerson = new Person();
Person secondPerson = firstPerson.myself; //Here you are referencing to same instance of Person class i.e. same `this`
But when you are creating new instance of Person
, it will refer to new this
但是当你创建Person
新实例时,它将引用new this
firstPerson.myself = new Person(); // New instance new `this`, but still `firstPerson` is referencing to previous instance
Explanation with diagram 用图解释
In your case you created new instance of person and stored in
myself
property. 在您的情况下,您创建了人的新实例并存储在myself
属性中。 butfirstPerson
andsecondPerson
is still pointing to samethis
instance 但firstPerson
和secondPerson
仍然指向同this
实例
The myself
is just a variable. myself
只是一个变数。 So when you call 所以当你打电话
Person firstPerson = new Person();
you have 2 variables which point to the same instance: firstPerson
and firstPerson.myself
. 你有两个指向同一个实例的变量: firstPerson
和firstPerson.myself
。 With line 随着线
Person secondPerson = firstPerson.myself;
you introduce third variable which still points to the same instance. 你介绍了仍然指向同一个实例的第三个变量。 Now with 现在用
firstPerson.myself = new Person();
you create second instance and make firstPerson.myself
point to this instance while the variables firstPerson
and secondPerson
still point to the first one. 你创建第二个实例并使firstPerson.myself
指向这个实例,而变量firstPerson
和secondPerson
仍然指向第一个实例。
1.Actually class variables are reference types . 1.实际上类变量是引用类型 。
2.So when you assign same instance to two variables then they will points to same instance. 2.因此,当您将同一实例分配给两个变量时,它们将指向同一个实例。
3.Whenever you what to point fresh one you need to use 'new' keyword for assignment. 3.无论何时你要指出什么都要新鲜,你需要使用'new'关键字进行分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.