NoviceToDotNet
NoviceToDotNet

Reputation: 10815

What does a null object mean?

If I create an object of a class

A obj = new A();

and later I assign null to it: obj = null;.

Then what does it mean obj being null? Does it mean now it is pointing nowhere and all memory is deallocated?

Upvotes: 1

Views: 5542

Answers (8)

Ken Kin
Ken Kin

Reputation: 4703

It means obj point to nowhere and original referenced instance of type A goes to gen0. If A is a type implemented IDisposable, then the resource that A holds are going to be released when Dispose called, and suppressed to be finalized. If a disposable is not explicitly disposed, then it would wait to be finalized.

If the execution afterwards reference it again, and it comes gen1 and then possibly gen2, it's life time are going to be longer.

The time when gc comes to collect garbages, the gen0s would be then first choice to be collected, then gen1, gen2.

Upvotes: 2

Code Enthusiastic
Code Enthusiastic

Reputation: 2847

A obj = new A();

The reference variable obj is of type A. You are creating an instance of A and assigning the reference of it to obj. obj now references the instance of A.

obj = null

The above assignment says the obj now references null. That is nothing. Now the newly created instance of A is out of reference and it will be eventually garbage collected.

Upvotes: 2

Joni
Joni

Reputation: 111379

null is not an object. It is a special reference value that references nothing. If you try to use a null reference to communicate with an object an error is thrown, because there is no object.

There is also a design pattern called the Null Object Pattern, created to solve the problem of the null reference requiring special cases. A null object in this pattern is an object that has "neutral" behaviour; that is, that when asked to do something actually does nothing, and when asked to return a value returns zeros, empty strings, empty lists, and other safe, neutral objects.

Don't confuse the null object with the special null reference.

Upvotes: 3

Aniket Inge
Aniket Inge

Reputation: 25723

Technically, obj is an alternative name for the memory location on heap that contains an object of type A when you say A obj = new A();

But if you then say obj = null; then obj now references nowhere.

Also the memory that was previously occupied by the object of type A, for which obj was an alias, that will be given back to the operating system by the GC when the GC thinks it is the right time.

Upvotes: 2

antonijn
antonijn

Reputation: 5760

You need to understand that reference types (classes) are in fact pointers to data in-memory. Essentially, what it does is set the pointer (which is just a piece of memory containing the address of the data) to the value zero, or null.

In practice, this means that if no other references to the data remain, the garbace collector may remove it at any point.

Upvotes: 2

Royi Namir
Royi Namir

Reputation: 148744

Internally it means that this object in not considered as a root. and will not be added to the available objects graph.

Hence , it will be considered as collect'able. ( non-deterministic time.).

Upvotes: 2

Oded
Oded

Reputation: 499352

obj now points to the null reference memory location.

From null (C# Reference) on MSDN:

The null keyword is a literal that represents a null reference, one that does not refer to any object.

If you have not assigned the object it formerly pointed to to anything else, that object will be eligible for collection by the GC.

Upvotes: 8

Roger Rowland
Roger Rowland

Reputation: 26279

It means it's pointing nowhere and the memory is eligible for deallocation by the Garbage Collector at some undefined time in the future.

Upvotes: 4

Related Questions