可能重复:
C# okay with comparing value types to null为什么要在C#中将结构与NULL合法比较?
考虑与时间跨度,这是一个结构下面的代码:
// will not compile - illegal
TimeSpan ts = null;
但是,下面的代码不会编译和合法,尽管表达总是假的:
if (ts == null)
Console.WriteLine("this line will never be hit");
有人可以告诉我为什么将结构设置为NULL是无效的,但可以将它与一个结果比较吗?
http://stackoverflow.com/questions/2022425/comparing-structs-to-null,http://stackoverflow.com/questions/648115/how-can-an-object-not-be-比较为null,http://stackoverflow.com/questions/1225949/why-can-timespan-and-guid-structs-be-compared-to-null,http://stackoverflow.com/questions/1972262/ c-sharp-okay-with-comparison-value-types-null- – nawfal 2012-04-09 23:44:31
如果无法将结构与null进行比较,那么将如何处理Nullable? –
2012-04-09 23:46:19
@AnthonyPegram:更好的问题是;你将如何实现'Nullable'作为'struct'? (当然,你不能)。 'Nullable ''当然可以作为参考类型来实现 –
2012-04-10 00:02:06