Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'struct' restriction mean?

The is a 'struct' restriction in generic classes or methods in C#. I want to know It means structs only or any type derived from value type like int, double, enum, ant so on. Is the next code let me to use simple types?

class SomeGenericClass <T> where T : struct
{
 //some inplementation
}
like image 673
Vasya Avatar asked Jan 29 '26 05:01

Vasya


1 Answers

What does 'struct' restriction mean?

It means any non-nullable value type. All structs are value types.

I want to know It means structs only or any type derived from value type like int, double, enum, ant so on. Is the next code let me to use simple types?

Your so-called "simple types", like int, double and enum are nothing more than C# keywords that correspond to the System.Int32 and System.Double structs, and System.Enum class which is based on System.ValueType (which makes enums also value types, despite Enum being a class itself).

Therefore these types also satisfy the where T : struct constraint, along with regular structs.

like image 196
BoltClock Avatar answered Jan 30 '26 19:01

BoltClock