为什么比较DateTimeVariable == null不会导致编译器错误或警告?

时间:2019-06-13 20:33:36

标签: c# .net

如果我声明一个DateTime变量(不是一个可以为空的DateTime?),则无法按预期将其设置为null,但是当我将其与null进行比较时,不会收到关于比较的硬错误或软警告,永远不会是真的。我在Visual Studio 2017和2008中进行了尝试,并得到了相同的行为。

DateTime payPeriodStart = DateTime.Now;

// This statement is not allowed as expected.
//payPeriodStart = null;

// This statement does not generate an error or a compiler warning,
// whereas doing this with any other struct generates an error.
if (payPeriodStart != null)
{
    // Do something.
}

0 个答案:

没有答案