'C# switch expressions null case
I've done some research, but have not found an answer to this. Is there a to represent the null case in the c# 8 switch expressions in such a way that the compiler will recognize and not trigger a warning for the reference x
in the base case when calling ToString()
? It seems like this is an obvious case and that I should not need to use the null forgiving operator !
in this case.
public override int? ConvertToInt(object? value) => value switch
{
var x when x == null => null,
int x => x,
var x => int.Parse(x!.ToString())
};
I have a feeling that they just haven't got to this yet, but I figured I'd toss the question out there.
Edit:
I did come up with a way to eliminate the need for the null forgiving operator, but I'm still curious as to if there's a specific null case syntax that is recognized. This doesn't feel like the best way as it's not completely clear, and I'm not even sure if this will be honored as I don't think Nullable references actually affect anything at run time, I will test this shortly.
public override int? ConvertToInt(object? value) => value switch
{
int x => x,
// notice the non-nullable object
object x => int.Parse(x.ToString()),
_ => null
};
Edit 2:
It looks like I was mistaken, this does seem to be honored. When running the following test the assertion did not fail.
[TestMethod]
public void MyTestMethod()
{
object? could_be_null = null;
string? str = could_be_null switch
{
object x => x.ToString(),
_ => null
};
Assert.IsNull(str);
}
Solution 1:[1]
After a bit of experimentation as displayed in the edits of the original question I came up with this solution. To my surprise the nullability of reference typed is checked at run time. This is the best that I could come up with, if someone can come up with something better, or there is an official syntax for the null case that I'm unaware of, I'll gladly accept your answer over mine.
public override int? ConvertToInt(object? value) => value switch
{
int x => x,
// notice the non-nullable object
object x => int.Parse(x.ToString()),
_ => null
};
Solution 2:[2]
I also pondered about this some time ago. Specifically, around whether to use a ternary operator or the new switch expression which respects and checks the nullability of reference types at runtime.
Example, which do you prefer, switch expression:
private void SetProductCategoryHierarchyFromParentNode() => ProductCategoryHierarchy =
ParentNode switch {
ProductCategoryHierarchyNode node => node.ProductCategoryHierarchy,
_ => ProductCategoryHierarchy.Default,
};
};
or ternary operator:
private void SetProductCategoryHierarchyFromParentNode() => ProductCategoryHierarchy =
ParentNode is not null
? ParentNode.ProductCategoryHierarchy
: ProductCategoryHierarchy.Default;
I would go for any of them, but one thing to note is that with the switch expression, one could easily introduce structural validation by pattern matching at a later time. For example:
private void SetProductCategoryHierarchyFromParentNode() => ProductCategoryHierarchy =
ParentNode switch {
{ HierarchyLevel: > 10 } => throw new Exception("Product category hierarchies deeper than 10 levels are strictly prohibited."),
ProductCategoryHierarchyNode node => node.ProductCategoryHierarchy,
_ => ProductCategoryHierarchy.Default,
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Paul-Sebastian |