C#字典值引用类型-请解释为什么会发生这种情况 [英] C# dictionary value reference type - please explain why this happens

查看:67
本文介绍了C#字典值引用类型-请解释为什么会发生这种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不了解C#中以下linqpad查询的结果.这些评论应该可以解释我的困惑之处.

I don't understand the results of the following linqpad query in C#. The comments should explain where I am confused.

void Main()
{
    Dictionary<string, testClass> test = new Dictionary<string, testClass>();

    string key = "key";
    testClass val = null;

    test.Add(key, val);

    val = new testClass();

    test[key].Dump(); //returns null.   WHAT? I just set it!!!



    test[key] = val;
    val.Text = "something";
    //  returns val object, with Text set to "Something". 
    //  If the above didn't work, why does this work?
    test[key].Dump(); 



    val.Text = "Nothing";
    //  return val object, with Text set to "Nothing". 
    //  This, I expect, but, again, why didn't the first example work?
    test[key].Dump(); 



    val = null;
    //  returns val object, with Text set to "Nothing"...WHAT?? 
    //  Now my head is going to explode...
    test[key].Dump(); 

}

// Define other methods and classes here

public class testClass
{
    public override string ToString()
    {
        return Text;
    }

    public string Text { get; set;}     
}

推荐答案

主要原因是变量(val)不是对象.它仅包含对对象的引用(或null).

The main reason is that the variable (val) is not the object. It merely contains a reference to an object (or null).

testClass val = null声明一个类型为testClass的变量,并将其设置为null.它没有指向任何对象.

testClass val = null declares a variable of type testClass and sets it to null. It doesn't point to any object.

test.Add(key, val)在字典中添加一个指向null的条目(注意:不是指向val也不指向任何对象).

test.Add(key, val) adds an entry in the dictionary that points to null (note: it doesn't point to val nor does it point to any object).

val = new testClass();创建testClass new 实例,并且val现在指向该新对象. test[key] 仍然为空,并且不指向任何对象.

val = new testClass(); creates a new instance of testClass and val now points to that new object. test[key] is still null and doesn't point to any object.

test[key] = val;
val.Text = "something";
test[key].Dump(); 

此代码将test[key]指向val指向的同一对象.再次注意是不是指向val .当使用val.Text = "something"更改对象时,可以使用test[key].Dump()看到更改,因为它们都指向同一个对象.

This code points test[key] to the same object that val points to. Again note is it not pointing to val. When you change the object with val.Text = "something" you can see the change using test[key].Dump() because they both point to the same object.

val.Text = "Nothing";
test[key].Dump(); 

val.Text设置为字符串"Nothing"时,由于与上述相同的原因,您可以通过test[key]看到更改,它们都指向同一对象.

When you set val.Text to the string "Nothing" you can see the change through test[key] for the same reason as above, they both point to the same object.

val = null;
test[key].Dump(); 

此代码将val设置为指向空. test[key] still 指向对象.现在valtest[key]指向不同的事物.

This code sets val to point at null. test[key] still points at the object. Now val and test[key] point to different things.

这篇关于C#字典值引用类型-请解释为什么会发生这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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