Extension method resolution with nullable value type params

后端 未结 5 1863
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  无人及你
    2021-01-19 06:37

    As Tim said nullable +1

    Fix:

      public static class Extension
      {
        public static void Test(this DateTime? dt)
        {
        }
      }
    
      public class Program
      {
        private void Main()
        {
          DateTime? now = DateTime.Now;
          Extension.Test(now); 
          now.Test();  
       }
      }
    

提交回复
热议问题