Setting a type reference type to null doesn't affect copied type?

后端 未结 5 1661
天涯浪人
天涯浪人 2020-12-09 12:57

Why does this produce \"0\" ?

object a = 0;
object b = a;
a = null;
Console.WriteLine(b.ToString()); // Produces \"0\"
Console.Read();

Does

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 13:42

    A picture is worth a thousand words:

    enter image description here

    Setting a = null removes a's reference to the object (the boxed integer 0). It does not affect the object itself. b still references the unchanged object afterwards.

提交回复
热议问题