Why can't I write Nullable>?

前端 未结 5 1166
陌清茗
陌清茗 2020-12-06 17:16

The definition of Nullable is:

[SerializableAttribute]
public struct Nullable where T : struct, new()

The constraint

5条回答
  •  余生分开走
    2020-12-06 18:12

    From section 4.1.10 of the C# language spec:

    A non-nullable value type conversely is any value type other than System.Nullable and its shorthand T? (for any T), plus any type parameter that is constrained to be a non-nullable value type (that is, any type parameter with a struct constraint). The System.Nullable type specifies the value type constraint for T (§10.1.5), which means that the underlying type of a nullable type can be any non-nullable value type. The underlying type of a nullable type cannot be a nullable type or a reference type. For example, int?? and string? are invalid types.

提交回复
热议问题