I am getting the Error:
A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'T'
while trying to write this piece of code
protected T GetValue<T>(Expression<Func<T>> property, T defaultValueIfNull = null);
Does anybody has idea that how to make null value types. Is there anyway to do this?
There are no constraints on type T
, so it can be a value type.
You can rewrite method definition as
protected T GetValue<T>(Expression<Func<T>> property, T defaultValueIfNull = default(T));
Which will mean null
for reference types and default value for value types.
T
in this case might also be a value type, such as int
, which cannot be null
. You should specify a type constraint, limiting T
to classes:
...T defaultValueIfNull = null) where T : class
An alternative would be using ...T defaultValueIfNull = default(T))
- you wouldn't need the constraint, but value types would become 0
by default, instead of null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With