日期时间的区别?和可以为空的<DateTime>

Lam*_*fif -3 .net c# datetime

我需要知道它们之间是否存在差异

DateTime? obj
Run Code Online (Sandbox Code Playgroud)

Nullable<DateTime> obj
Run Code Online (Sandbox Code Playgroud)

如果是这样的话,他们的用例是什么?

Dav*_*d L 8

没有区别.前者是第二种语法糖.他们编译完全相同的东西Nullable<DateTime>,建立相同的IL.

void Main()
{
    DateTime? test1 = DateTime.Now;
    Nullable<DateTime> test2 = DateTime.Now;
}
Run Code Online (Sandbox Code Playgroud)

IL for DateTime?

IL_0001:  ldloca.s    00 // test1
IL_0003:  call        System.DateTime.get_Now
IL_0008:  call        System.Nullable<System.DateTime>..ctor
Run Code Online (Sandbox Code Playgroud)

IL for Nullable<DateTime>

IL_000E:  ldloca.s    01 // test2
IL_0010:  call        System.DateTime.get_Now
IL_0015:  call        System.Nullable<System.DateTime>..ctor
Run Code Online (Sandbox Code Playgroud)