Unlike the specialized specialization of C ++, there is no such specialization as the specialization in .Net.
You cannot initialize an instance of T for anything other than T unless you have a restriction, so you cannot use a static constructor to try to work around the problem.
As far as I know, there are no restrictions, which allows you to specify only types that are either numeric types or have conversions from numeric types.
I suggest you just make two different types of classes:
public struct PixelFloat { public float R { get; set; } public float G { get; set; } public float B { get; set; } public const float Min = 0.0f; public const float Max = 1.0f; } public struct PixelDouble { public double R { get; set; } public double G { get; set; } public double B { get; set; } public const double Min = 0.0f; public const double Max = 1.0f; }
This is actually the same thing, since this is what he compiled under the covers. Other solutions will not buy you compared to this, because you still have to type in: Pixel<double> Pixel<float> .
In addition, in such cases, I suggest you use names that indicate that you are using types from your general parameters. Size not, obviously, a general parameter. TSize is. And TSize does not describe what the type does, it just describes how it changes. Instead, you should call it something like TValue .
source share