Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to Creating a Null Object in c# and what it use???
Posted
Updated 20-Nov-11 20:11pm
v2
Comments
Sergey Alexandrovich Kryukov 21-Nov-11 2:30am    
Object @object = new null; care to try? :-)
--SA
Sander Rossel 21-Nov-11 2:36am    
My vote of 1, doesn't compile ;-)

1 solution

If you are meaning assigning null to an Object[^]:
This can be used to explicitly eliminate a reference to an Object[^].
C#
SomeObject so = new SomeObject();
so.DoSomething();
so.Dispose();
so = null; // We are now absolutely sure the SomeObject so was referencing to is no longer referenced by so.
// This should theoretically not be necessary in .NET.

Create a Nullable Object:
It is possible to create Nullable types[^] of value types that would otherwise not allow a null reference.
C#
Nullable<int> i1 = null; // Actually has a null reference.
int? i2 = null; // Same as code above.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 21-Nov-11 2:29am    
First sample won't compile. I cannot suspect you don't know this, because you reference an article about nullable types. Also, assigning null even to a nullable object does not have the semantic of default value. Wow! If this is a joke, you should disclose it somewhere, because OP is so naive...
--SA
Sander Rossel 21-Nov-11 2:32am    
Argh, you're right SA, got it out of my answer.
That stuff only works in VB... *shame, shame*
It's still early ;)
Sergey Alexandrovich Kryukov 21-Nov-11 2:32am    
You should have explained that a null can be assigned only to a variable of a reference type, including the nullable.
--SA
Sander Rossel 21-Nov-11 2:36am    
I quote from my (original) answer above: "value types that would otherwise not allow a null reference"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900