-2
User x = null;
object o = x;
// determine type with only reference to o

And Generics will NOT Work

Kyle
  • 1,172
  • 1
  • 8
  • 23
  • 1
    He's asking how to get the Type of a null object. – djdd87 Sep 06 '10 at 08:48
  • Hi question is how do I get the underlying type of a null object, but it could have been clearer and he could have been a little more polite. – DavidGouge Sep 06 '10 at 08:48
  • possible duplicate of [.NET : How do you get the Type of a null object?](http://stackoverflow.com/questions/254461/net-how-do-you-get-the-type-of-a-null-object) – djdd87 Sep 06 '10 at 08:51

5 Answers5

14

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

julian
  • 4,634
  • 10
  • 42
  • 59
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Oh yes, precisely why two null references (even of different types at compile time) are equal in .NET – nawfal Apr 25 '13 at 12:24
  • @Eric even though you say there 'there is no flavour to "nothing at all".', the compiler can still see 'flavors' of null when resolving these overloads: `MyMethod(Foo foo)` and `MyMethod(Bar bar)`, if you call them as `MyMethod(null as Foo)` and `MyMethod(null as Bar)`. So doesn't this legitimize the question of how to resolve the runtime flavor of null within MyMethod (e.g. if someone call 'MyMethod(null as BarDerived)`)? – GDS Dec 12 '16 at 07:57
  • @GDS: You are misunderstanding how this works. `null as Bar` is an expression with compile time type Bar, exactly the same as `Bar bar = null; M(bar);` But the thing that has the type Bar is the *variable* in the latter case or the *expression* in your case, not the *referent of the reference*. There is no referent of the reference. Again, it's like an empty box; the box can have a restriction on what kinds of things go in it, but it's still an empty box. – Eric Lippert Dec 12 '16 at 15:14
  • @Eric: This is getting theoretical now, but couldn't null have been implemented as a reference to THE null object of the specific type (one per type)? Invocation of members and casting(?) could still throw exceptions but runtime type info would be available. – GDS Dec 12 '16 at 21:40
  • 1
    @GDS: Sure, that's called the "null object pattern". https://en.wikipedia.org/wiki/Null_Object_pattern I use this pattern frequently. Feel free to use it if you like it. – Eric Lippert Dec 12 '16 at 21:53
9

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.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
1

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.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
0

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.

tia
  • 9,518
  • 1
  • 30
  • 44
0

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 Uris 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.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251