4 ms·
You can also constrain a generic type only to value types in C#: class Result<T> where T: struct { ... } In that case it can't be null with C# either.
by sedatk 6y ago
You can also constrain a generic type only to value types in C#:
class Result<T> where T: struct
{
...
}
In that case it can't be null with C# either.
- AaronFriel 6y agoThen you can't construct it unless it's successful, no? A Result<T> that can only contain successful values doesn't seem very useful
- sedatk 6y agoYou can, it's possible to address "missing values" with a default construct. Example: int x = default; // x becomes zero T x = default; // x becomes whatever the default value for struct is
- AaronFriel 6y agoThen we're back to accessing that value being an enormous footgun, yes?