在设计一个返回类型为T的泛型方法时,有时希望能返回空Null,然后会报错:

【C#】允许泛型方法<T>返回空值Null

根据提示,将返回值由Null改为default(T)即可。
default(T)表示返回当前T类型的默认值,如果T为int则返回0。为了使确实能返回Null,可将该T类型继承自class类 where T : class,变成只能返回引用类型。

重要参考:

http://stackoverflow.com/questions/302096/how-can-i-return-null-from-a-generic-method-in-c

 

相关文章:

  • 2022-12-23
  • 2021-10-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-16
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
相关资源
相似解决方案