我收到错误:
A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'T'
在尝试编写这段代码时
protected T GetValue<T>(Expression<Func<T>> property, T defaultValueIfNull = null);
有没有人知道如何制作空值类型。有办法做到这一点吗?
最佳答案
类型T
没有限制,所以可以是值类型。
您可以将方法定义重写为
protected T GetValue<T>(Expression<Func<T>> property, T defaultValueIfNull = default(T));
这意味着引用类型为 null
,值类型为默认值。
关于c# - '<null>' 类型的值不能用作默认参数,因为没有到 'T' 类型的标准转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32732200/