How to pass a value to a C# generic?

.net, c#, generics

Solution

C# generics are not like C++ templates in that way. They only work with types and not values.

Problem

In C++ templates have the feature that you can pass a value as the argument to the template of a function. How can I do the same in C#? For instance, I want to do something similar to the following: ``` template <unsigned n> struct Factorial { enum { result = n * Factorial<n - 1>::result; }; }; template <> struct Factorial<0> { enum { result = 1; }; }; ``` but in C#. How can I do this? By the way, my actual need for them involves generating classes on demand (with a few static values changed), so the presented code is just an example.

Original source

Related problems