In the previous post I mentioned that C# pattern matching is far from complete. In particular, close coupling between matching and deconstruction is not present so far in C#. The maximum what can be achieved using deconstruction is extraction of values from complex types:
(var name, var color, _) = new Cat();
Underscore here represents a value to discard, similar to out variables. However, what I would really like to see from the pattern matching is the following:
if (x is Cat (var name, var color, _)) { }
to match x
as Cat
and deconstruct it to extract name and color. Or
if (x is Cat (var name, var color, 2)) { }
to match x
as two years old Cat
, and extract the rest if it is true. Or may be
If (GetCat() is (_, var color, 3)) { }
to match and deconstruct the Cat
object returned by GetCat()
All these constructs bring together pattern matching, tuples and deconstruction and allow to develop even more condensed code.
blog comments powered by Disqus