I have the following generic method, but VS gives me a compile error on that. (Operator \'??\' cannot be applied to operands of type \'T\' and \'T\')
public stat
For some reason the ??
operator can't be used on non-nullable types, even though it is supposed to be equivalent to model == null ? new T() : model
, and you are allowed a null comparison with a non-nullable type.
You can get exactly what you're looking for without any additional constraints by using the ternary operator instead, or an if statement:
public static T Method(T model) where T : new()
{
var m = model == null ? new T() : model;
}