I have seen code with the following logic in a few places:
public void func()
{
_myDictonary["foo"] = null;
_myDictionary.Remove("foo");
}
What is the point of setting foo to null in the dictionary before removing it?
I thought the garbage collection cares about the number of things pointing to whatever *foo originally was. If that's the case, wouldn't setting myDictonary["foo"] to null simply decrease the count by one? Wouldn't the same thing happen once myDictonary.Remove("foo") is called?
What is the point of _myDictonary["foo"] = null;
edit: To clarify - when I said "remove the count by one" I meant the following:
- myDictonary["foo"] originally points to an object. That means the object has one or more things referencing it.
- Once myDictonary["foo"] is set to null it is no longer referencing said object. This means that object has one less thing referencing it.
The reference disappears, the object continues to live.
The metonymy "going out of scope" is used to express the idea that the dynamic activation/instantiation of the environment associated with some scope is terminating. And so the variables defined in that scope are going away (thus "out of scope").
Destructor is called when the object of a class goes out of the scope in the program. The cases when object goes out of scope, The program goes out of the scope of a function. The program ends.
Here Foo bar = new Foo(); you created object of Foo and put reference to the variable bar . Here Foo a = bar; and Foo b = bar; you put the reference to the variables a and b . So you have now one object and three variables pointing to him. Here bar = null; you clear the bar variable.
There is no point at all.
If you look at what the Remove
method does (using .NET Reflector), you will find this:
this.entries[i].value = default(TValue);
That line sets the value of the dictionary item to null, as the default value for a reference type is null. So, as the Remove
method sets the reference to null, there is no point to do it before calling the method.
Setting a dictionary entry to null
does not decrease the ref count, as null
is a perfectly suitable value to point to in a dictionary.
The two statements do very different things. Setting the value to null
indicates that that is what the value should be for that key, whereas removing that key from the dictionary indicates that it should no longer be there.
There isn't much point to it.
However, if the Remove
method causes heap allocations, and if the stored value is large, a garbage collection can happen when you call Remove
, and it can also collect the value in the process (potentially freeing up memory). Practically, though, people don't usually worry about small things like this, unless it's been shown to be useful.
Edit:
Forgot to mention: Ideally, the dictionary itself should worry about its own implementation like this, not the caller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With