Nullable implementation

前端 未结 3 468
暗喜
暗喜 2021-01-22 10:27

I am trying to implement Nullable type. But below mentioned code doesn\'t support null value for valuetype datatypes.

using System;
using System.Runtime;
using S         


        
3条回答
  •  无人共我
    2021-01-22 10:49

    Assigning null to Nullable is just a syntactic sugar for assigning new Nullable(), it's part of C# language, and you can't add that feature to you custom type.

    C# spec,

    4.1.10 Nullable types

    A nullable type can represent all values of its underlying type plus an additional null value. A nullable type is written T?, where T is the underlying type. This syntax is shorthand for System.Nullable, and the two forms can be used interchangeably.

    6.1.5 Null literal conversions

    An implicit conversion exists from the null literal to any nullable type. This conversion produces the null value (§4.1.10) of the given nullable type.

提交回复
热议问题