具有可选类型的 WebApi 模型

WebApi model with optional type

我正在用 C# 编写一个模型,我希望它具有另一个模型的可为 null 的 List<>。 所以我尝试了public List<Location>? locations { get; set; }。 然而,这错误 List<Location> 必须是不可空的才能将其用作 Nullable<T>.

中的类型 T

现在我也尝试了 public virtual List<Location>? locations { get; set; },但它以同样的方式出错。我应该只使用 List<Location>,并且在没有位置时将其留空吗?

A​​ List<T> 派生自 object 所以它的默认类型已经是 null

只有值类型需要可为 null 的包装器,例如 int?Nullable<DateTime>