By Priest

2011-11-09 17:59:40 8 Comments

Why does this produce "0" ?

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

Doesn't b point to the same location and setting a = null effectively makes b null?


@Eric Lippert 2011-11-09 18:50:41

You want to know where the cookies are. You have a piece of paper, labelled "A". On the paper is written in pencil "123 Sesame Street".

The paper is not a cookie. The address is not a cookie. The paper contains a reference to an address which contains the cookie.

You obtain a second piece of paper, labelled "B". On that piece of paper, you make a copy of the contents of "A". Now you have two pieces of paper, both say "123 Sesame Street". Both tell you where the cookies are.

You take piece of paper "A" and erase it. "A" no longer refers to the location of the cookies. B still does.

You are assuming that saying "b = a" means to write on B "for the location of the cookies, please consult paper A". But that is not what "b = a" means in C#; it means make a copy of the reference, not make an alias of the reference.

In C# to make an alias of the reference you use the "ref" keyword, confusingly enough:

void M(ref object b)
    b = null;
object a = 0;
M(ref a);
// "b" now becomes an alias for "a"; when "b" is nulled out, so is "a" because they are the same variable with two different names.

In C# you can only do this when calling a method that takes a ref parameter like this. The feature you want is not supported in C#, though we have considered supporting it:

object a = 0;
ref object b = ref a;
a = null; // b and a are aliases for the same variable now.

Do you have a compelling need for this feature? If you do, please let me know what it is. That will help us prioritize whether or not the feature is worth doing in a hypothetical future version of C#.

UPDATE: It got done! This feature was added to C# 7.

@phoog 2011-11-09 23:43:36

I'm not sure that the OP is assuming that "b = a" means "for the location of the cookies, please consult paper A". I read the post as assuming that "a = null" means "remove the cookies from the location on paper A".

@TamerM 2015-04-02 12:19:40

I have a question, after setting "a" to NULL, will there still be an entry in the stack named "a" with an empty value, or does setting it to NULL mean directly deleting the entry from the stack?

@Eric Lippert 2015-04-02 16:01:54

@TamerM: Setting a variable to null does not "delete" the variable; what if you tried to say if (a==null) later?

@TamerM 2015-04-05 06:13:05

Good Point. Thanks

@Olivier Jacot-Descombes 2017-03-17 13:25:24

Starting with C# 7.0 we have ref returns and ref locals!

@Olivier Jacot-Descombes 2011-11-09 18:27:11

A picture is worth a thousand words:

enter image description here

@abatishchev 2011-11-09 18:03:33

Here's a description of what's going on:

object a = 0; // pointer a = 0xSomeA
object b = a; // pointer b = 0xSomeB
a = null; // nulling a, now 0x00; b still the same

@Darin Dimitrov 2011-11-09 18:02:04

Boxing (which is the process that happens here) is pretty nicely explained in the following article with examples at what happens in memory.

@BrokenGlass 2011-11-09 18:01:16

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").

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] Setting Objects to Null/Nothing after use in .NET

12 Answered Questions

[SOLVED] How to use LINQ to select object with minimum or maximum property value

  • 2009-05-27 05:31:25
  • slolife
  • 219345 View
  • 404 Score
  • 12 Answer
  • Tags:   c# .net linq

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

12 Answered Questions

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

10 Answered Questions

[SOLVED] Is there a difference between "throw" and "throw ex"?

5 Answered Questions

5 Answered Questions

[SOLVED] Curious null-coalescing operator custom implicit conversion behaviour

6 Answered Questions

[SOLVED] C# String reference type passed as copy?

  • 2014-01-15 20:49:34
  • Alberto Montellano
  • 459 View
  • 0 Score
  • 6 Answer
  • Tags:   c# reference-type

7 Answered Questions

[SOLVED] Why are delegates reference types?

Sponsored Content