Extension method resolution with nullable value type params

后端 未结 5 1870
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 05:54
public static class Extension
{
    public static void Test(this DateTime? dt)
    {
    }
}


void Main()
{
    var now = DateTime.Now;
    Extension.Test(now); //          


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 06:51

    you need to create your variable now with the corret type nullable like this:

     DateTime? dateTime = DateTime.Now;
     dateTime.Test();
    

提交回复
热议问题