TOP KEK
TOP KEK

Reputation: 2651

Generic with T of value type

Is it possible to define a generic class where T can belong only to value type (such as int, double and so on)?

Upvotes: 0

Views: 112

Answers (2)

Selman Genç
Selman Genç

Reputation: 101681

yes, you need a struct constraint:

class OnlyStructs<T> where T : struct { }

But you should be aware of that this also allows user-defined structs, not just int,double etc.. Unfortunately there is no built-in way to restrict T for only specific types like where T : int,double,float.

Upvotes: 5

Sanja Melnichuk
Sanja Melnichuk

Reputation: 3505

Use struct in generic constraint where T : struct

Upvotes: 4

Related Questions