Generic Constraint for Non Nullable types

后端 未结 2 872
囚心锁ツ
囚心锁ツ 2021-01-07 22:01

I have the following class:

public class KeyDTO
{
     public T Id { get; set; }
}

So far so good, but I want the type parameter <

相关标签:
2条回答
  • 2021-01-07 22:43

    Applying where T : struct applies a generic constraint that T be a non-nullable value type. Since there are no non-nullable reference types, this has the exact same semantics as simply "all non-nullable types". Nullable value types (i.e. Nullable<T>) do not satisfy the struct generic constraint.

    0 讨论(0)
  • 2021-01-07 22:59

    From C# 8.0 you can now use the where T : notnull generic constraint to specificy T is a non-nullable type.

    0 讨论(0)
提交回复
热议问题