C#班?在财产(这是什么意思?)

Jac*_*ack 0 c# nullable

我正在为我的MCPD学习,这个示例类在ADO.net实体框架示例中显示.我还没遇到过?在OrderDate和EmployeeID等财产中,有人可以向我解释它的作用或含义吗?

public class Order
{
 public int OrderID { get; set; }

 public string CustomerID { get; set; }
 public int? EmployeeID { get; set; }
 public DateTime? OrderDate { get; set; }
 public DateTime? RequiredDate { get; set; }
 public DateTime? ShippedDate { get; set; }
 public int? ShipVia { get; set; }
 public decimal? Freight { get; set; }
 public string ShipName { get; set; }
 public string ShipAddress { get; set; }
 public string ShipCity { get; set; }
 public string ShipRegion { get; set; }
 public string ShipPostalCode { get; set; }
 public string ShipCountry { get; set; }
 public Customer Customer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

jas*_*son 7

这对于短类型的Nullable<T>地方T是,在之前的类型?.从而

public int? EmployeeID { get; set; }
Run Code Online (Sandbox Code Playgroud)

相当于

public Nullable<int> EmployeeID { get; set; }
Run Code Online (Sandbox Code Playgroud)

实际上,可空类型允许您分配null值类型.这些类型非常特殊,因为相应的非可空类型上的各种运算符和方法被"提升"为可空类型.

请注意,T必须是不可为空的值类型.此外,这是一种常见的误解,它Nullable<T>是一种参考类型.它是一个值类型,虽然是一个非常特殊的类型(它从编译器得到的帮助是如此特殊).例如,非常特殊地处理装箱和拆箱操作(底层值是装箱的,除非null在这种情况下装箱的实例objectnull引用;如果null未装箱到可空类型的实例,则将其取消装箱到其中的值HasValuefalse).