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

后端 未结 5 1659
天涯浪人
天涯浪人 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:39

    You are setting the reference to null, you are not changing the object the reference points to. a and b are two separate references, hence setting a to null will of course leave b unchanged (Think "pointer"), it just means that a now points to null ("nowhere").

提交回复
热议问题