If I have a nullable value type I always have to use its Value property, even after I checked it for null. Is there a consise way around it?
public void Foo(SomeStruct? s)
{
if (s != null)
{
DoIt(s.Value.x + s.Value.y + s.Value.z);
}
}
The obvious way would be to define a new variable which makes the code longer for each variable it affects and which I find makes the code harder to read:
if (s != null)
{
var sv = s.Value;
DoIt(sv.x + sv.y + sv.z);
}
One other thing that came to my mind is pattern matching, but this has the drawback of a runtime typecheck:
if (s is SomeStruct sv)
{
DoIt(sv.x + sv.y + sv.z);
}
Am I overlooking something (besides the fact that I maybe should avoid null variables in the first place)?