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

前端 未结 8 738
旧巷少年郎
旧巷少年郎 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:16

    ?? is the null-coalescing operator. It can't be applied to non-nullable types. Since T can be anything, it can be an int or other primitive, non-nullable type.

    If you add the condition where T : class (must be specified before new()) it forces T to be a class instance, which is nullable.

提交回复
热议问题