C# has been getting many new features in recent versions.
And one that stands out is pattern matching.
What is it?
Pattern matching lets you check if an object has specific characteristics:
- Is null or is not null
- Is of a particular type
- Property has a specific value
I enjoy using it for type checks, and I particularly like switch expressions.
However, the more complex the pattern is, the more I dislike it.
Sure, it can be concise, and you usually write less code.
But you also need to think about readability and maintainability.
P.S. If you liked this, join The .NET Weekly - my newsletter with 23k+ readers that teaches you how to improve at .NET & software architecture:
https://lnkd.in/dMDPXuUh
Take a look at the example from the code snippet.
Which version do you find more readable?
And one that stands out is pattern matching.
What is it?
Pattern matching lets you check if an object has specific characteristics:
- Is null or is not null
- Is of a particular type
- Property has a specific value
I enjoy using it for type checks, and I particularly like switch expressions.
However, the more complex the pattern is, the more I dislike it.
Sure, it can be concise, and you usually write less code.
But you also need to think about readability and maintainability.
P.S. If you liked this, join The .NET Weekly - my newsletter with 23k+ readers that teaches you how to improve at .NET & software architecture:
https://lnkd.in/dMDPXuUh
Take a look at the example from the code snippet.
Which version do you find more readable?