Getting the base type of a null object

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

And Generics DO NOT WORK

-2
source share
5 answers

Imagine you have a library of books. Imagine that you also have a box with cards, one card for each book. (Younger readers: libraries actually used such systems before computers made them obsolete.)

Now imagine that you have two trays. In one tray marked "Science Fiction", and in the other tray marked "Any book."

SF tray is empty.

Tell your assistant librarian: "Dump everything in any tray, and then take a photocopy of what is in the SF tray and put a copy in any tray."

After resetting any tray, it becomes empty, and since the SF tray is empty, photocopies do nothing, so any tray remains empty.

The analogue of your question is now "what is the genre of the book whose card is in any tray?" and the answer is “there is no such genre because“ Any tray is empty. ”It's not like the SF tray was empty, somehow“ infects ”any tray to make it“ empty, but SF-flavored. ”

It makes sense? Variables are just storage locations; null links are links that mean "it doesn’t link anything at all," and there is no taste for "nothing at all."

For more on this difference, see my article on this topic:

http://blogs.msdn.com/b/ericlippert/archive/2009/10/29/i-have-a-fit-but-a-lack-of-focus.aspx

+10
source

o is a null reference (note the wording here: this is a null reference, not a reference to a null object). Such links do not have different types. Thus, you cannot determine what type of variable that was initially set to null .

+9
source

I do not think you can do this. The Object.GetType () method, of course, will not work with zero, and the Type.GetType methods require a name or handle so that they do not work.

Why do you need this? Perhaps there will be another way to accomplish what you need.

+1
source

Null is not an object. Zero is not an instance. Null is just the language syntax to clear the reference variable and make a link to "nowhere."

You cannot get the type "null", just as you cannot describe what it looks like "nowhere."

0
source

null is a value that can be assigned to various types.

In the example in your question, the type x is User , and the type o is object . For this reason, you can compile Uri u = o , but not Uri u = x . At runtime, the first one will also work, because null is valid for Uri , but the object was not null and User object, this would be an error because this value could not be passed to Uri .

null means "this value is not a reference to an object somewhere in memory." Not being an object at any point in memory, it works regardless of type.

It is also not true that generics will not work, even if you use it in your question.

Generation works with types, not values. Enumerable.Repeat(x, 3) will return an IEnumerable<User> with three null elements, and Enumerable.Repeat(o, 3) will return an IEnumerable<User> IEnumerable<object> with three null elements. The fact that the types of enumerations are different shows that generics will work perfectly here.

0
source

Source: https://habr.com/ru/post/947524/


All Articles