Operator '??' cannot be applied to operands of type 'T' and 'T'

前端 未结 8 885
旧巷少年郎
旧巷少年郎 2021-02-06 20:34

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         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 21:06

    model ?? new T() means model == null ? new T() : model. It is not guaranteed that model is non-nullable and == cannot be applied for null and a non-nullable object. Changing constraint to where T : class, new() should work.

提交回复
热议问题