where T struct (value type)
Context: The constraint where T : struct requires the type parameter to be a non-nullable value type (int, double, bool, DateTime, enum, etc.). It excludes reference types (classes, interfaces, delegates). This constraint allows safe usage of value types.
Usage Example
Section titled “Usage Example”using System;
public class NumericCalculator<T> where T : struct{ public T Zero => default(T); // default(T) = 0 for int, false for bool, etc.
public T Add(T a, T b) { // For simplicity, use dynamic conversion (not ideal, but demonstration) dynamic da = a; dynamic db = b; return da + db; }}
class Program{ static void Main() { var calcInt = new NumericCalculator<int>(); Console.WriteLine(calcInt.Add(5, 3)); // 8
var calcDouble = new NumericCalculator<double>(); Console.WriteLine(calcDouble.Add(2.5, 1.2)); // 3.7
// This does not compile: string is a reference type // var calcString = new NumericCalculator<string>(); }}Output console
Section titled “Output console”dotnet run83.7Important notes
Section titled “Important notes”structimpliesSystem.ValueType.- Nullable value types (
int?) are not allowed (becauseNullable<T>is a struct but with special constraint). - Useful for mathematical types or low-level buffers.
Real-world usage example
Section titled “Real-world usage example”System.Nullable<T> – where T : struct guarantees that T is a value type.
See .NET docs on struct constraint.