Generic class with restricted Type parameter
c#, generics, types
Solution
Unfortunately this is not possible (see here.) You can only constrain the type to:
- Implement a specific interface or derive from a specific class
- Be a `class` or `struct`
- Have a parameterless constructor
Constraining types to have specific operators is a much-requested feature but I believe it will not be in C# 4 either.
Problem
I want to create a generic class that takes a type parameter and restrict that parameter to numeric types or more generally to any type upon which the increment operator ++ can be applied. I know I can do the following to restrict to structs but obviously there are structs that aren't numeric types and for which the ++ operator is not supported. Can I do this in C# ``` class Example<T> where T : struct { //Implementation detail } ```