Given the following class
public class Component<TValue>
{
public TValue? MaxValue { get; set; }
public TValue? MinValue { get; set; }
public TValue Value { get; set; } = default!;
public override string ToString() =>
$"MinValue = {MinValue}, MaxValue = {MaxValue}, Value = {Value}";
}
Why is it that when I create an instance of Component<int>
the MinValue
and MaxValue
properties are int
instead of Nullable<int>
?
static void Main(string[] args)
{
var instance = new Component<int>();
instance.Value = 42;
Console.WriteLine(instance.ToString());
}
I could get it to behave how I want to by adding where TValue: struct
, but I am writing a Blazor component that needs to work with any numeric type (including nullables) so I can't add that constraint.
I'd like to know the logic behind why TValue?
for an int
should not be compiled as Nullable<Int32>
.