本文介绍了获取基础类型的空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

SF纸盒是空的。

您告诉您的助理馆员转储无论是在任何托盘,然后作出的无论是在SF托盘复印件,并把副本中的任何托盘。

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

为之倾倒变空,因为SF纸盒是空的没有什么可影印,因此任何托盘保持为空。

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.

你的问题的类似物是现在什么是他的卡是在任何托盘中的书籍的类型?答案是有没有这样的曲风,因为的的任何纸盘为空的。它不喜欢的事实,SF托盘是空的某种方式感染的任何托盘,使之空,但SF味。

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:

这篇关于获取基础类型的空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:54