Nullable implementation

前端 未结 3 499
暗喜
暗喜 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 11:09

    You are declaring your homegrown Nullable as a struct, and a struct is not nullable. You should declare it as a class instead.

    this code should throw the same error you are having, switching the Point type declaration from struct to class should fix it.

    void Main()
    {
        Point p = null;
    }
    
    // Define other methods and classes here
    struct Point
    {
     public int X   {get; set;}
     public int Y   {get; set;}
    }
    

提交回复
热议问题