User x = null;
object o = x;
// determine type with only reference to o
And Generics will NOT Work
User x = null;
object o = x;
// determine type with only reference to o
And Generics will NOT Work
Imagine you have a library of books. Imagine you also have a box of cards, one card for each book. (Younger readers: libraries actually used to have such systems, before computers made them obsolete.)
Now imagine you have two trays. One tray is marked "Science Fiction" and the other tray is marked "Any book".
The SF tray is empty.
You tell your assistant librarian "Dump whatever is in the Any tray, then make a photocopy of whatever is in the SF tray and put the copy in the Any tray."
After dumping the Any tray it becomes empty, and since the SF tray is empty there is nothing to photocopy, therefore the Any tray stays empty.
The analogue of your question is now "what is the genre of the book whose card is in the Any tray?" and the answer is "there is no such genre because the Any tray is empty". It's not like the fact that the SF tray was empty somehow "infects" the Any tray to make it "empty but SF flavoured".
Does that make sense? Variables are just storage locations; null references are references that mean "this doesn't reference anything at all", and there is no flavour to "nothing at all".
For more on this distinction see my article on the subject:
http://blogs.msdn.com/b/ericlippert/archive/2009/10/29/i-have-a-fit-but-a-lack-of-focus.aspx
o
is a null
reference (note the wording here; it's null reference, not a reference to a null object). Such references does not come in different types. So you cannot determine what type the variable that originally was assigned the null
value is.
I don't think you can do this. The Object.GetType() method will of course not work on a null, and the Type.GetType methods require a name or a handle so they won't work either.
What do you need it for anyway? There might be another way to accomplish what you are after.
Null is not an object. Null is not an instance. Null is just language syntax to clear the reference variable and make it reference to 'nowhere'.
You cannot get the type of 'null', as in the same way you cannot describe how 'nowhere' looks like.
null is a value that can be assigned to different types.
In the example in your question, the type of x is User
and the type of o is object
. For this reason you could compile Uri u = o
but not Uri u = x
. On runtime the former would also work, because the null value is allowed for Uri
s but the object had not been null but a User
object, it would error, as that value could not be cast to a Uri
.
null means "this value is not a reference to an object somewhere in memory". Not being an object anywhere in memory works the same whatever the type.
It's also not true that generics will not work, even if you capitalise it in your question.
Generics work on types, not values. Enumerable.Repeat(x, 3)
will return an IEnumerable<User>
with 3 null items, while Enumerable.Repeat(o, 3)
will return an IEnumerable<object>
with 3 null items. That the types of the enumerations are different shows that generics will work perfectly here.