News

Deborah Kurata's Favorite 'New-ish' C# Feature: Pattern Matching

We often hear about the big new features in .NET or C#. But what about all of those lesser-known but useful new features like collection indices and ranges, date features, pattern matching and records?

C# coders who want to stay current with the state of their favorite programming language might have a hard time keeping up with its "new-ish" features as it rapidly evolves -- and likewise with .NET -- which is why we have Deborah Kurata.

The Microsoft MVP, Google Developer Expert (GDE), consultant, author and speaker will be explaining those features in a presentation at the five-day, in-person Visual Studio Live! developer conference taking place May 15-19 in Nashville. She will share her multi-discipline expertise in a session titled Useful New-ish Features in .NET/C#.

Attendees of the presentation will learn:

  • About some of the new-ish features in C#
  • About some of the new-ish features in .NET
  • How to leverage these features in your current projects

We caught up with the very busy Kurata to learn more about her presentation, in which she promises to discuss how new-ish features in C# minimize the amount of code a developer needs to write, and later read and maintain. These include top-level statements, global using directives, implicit usings and file-scoped namespaces. One preview feature in the upcoming C# 12, for example, is a primary constructor, which simplifies how devs build constructors for their classes.

However, one C# feature especially favored by Kurata is pattern matching, explained by Microsoft thusly:

Pattern matching is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The "is expression" supports pattern matching to test an expression and conditionally declare a new variable to the result of that expression. The "switch expression" enables you to perform actions based on the first matching pattern for an expression. These two expressions support a rich vocabulary of patterns.

She explained that pattern matching isn't exactly new, but is certainly -ish.

"Pattern matching was new in C# 7 and has been enhanced through many of the recent C# versions, including C# 10 and C# 11," Kurata told Visual Studio Magazine.

Inside the Session

What: Useful New-ish Features in .NET/C#

When: May 16, 8 a.m. - 9:15 a.m.

Who: Deborah Kurata, Microsoft MVP and Google Developer Expert (GDE) Consultant, Speaker, Author

Why: Learn why would want to use new-ish .NET and C# features to enhance and simplify your code.

Find out more about Visual Studio Live!, taking place May 15-19 in Nashville

She explained that when she first discovered pattern matching, she was a bit lost in the terms, pattern names and syntax, and so didn't think the feature would be useful in her day-to-day coding. But after spending some time diving into the feature's purpose, she realized she was missing a great opportunity to simplify her code.

"Most of our applications have lots of logic," she said. "We have business logic such as if the user has good credit and they're buying a new vehicle then ..., but if they have moderate credit and buying a used vehicle then ... and so on. We have validation logic such as if the model is a 3, the performance package is not allowed and only five passengers. And display logic such as if the user clicked a sort order and a filter then ..., but if the user clicked a sort order with no filter then ... and so on."

Pattern matching, she said, can dramatically simplify and clarify such "if" logic, making code easier to read, understand, modify and test. An added benefit is that the compiler will tell you if you're missing any cases, which is a huge benefit when dealing with complex logic.

"We can use pattern matching to match on Boolean values, string values, tuples, arrays, and object properties," Kurata said.

"This provides a tremendous amount of power and flexibility for implementing and simplifying our logic."

Deborah Kurata, Microsoft MVP and Google Developer Expert (GDE), Consultant, Speaker, Author

"This provides a tremendous amount of power and flexibility for implementing and simplifying our logic."

Furthermore, she said, "One of the newest pattern matching features is the ability to match an array or list against a sequence of patterns. A common scenario for using this pattern is for processing CSV files."

Along with pattern matching, Kurata will explain what many of the other new-ish features are, how to use them and, more importantly, why you would want to use them to enhance and simplify your code. Her 75-minute session starts at 8 a.m. on May 16, the second day of the conference.

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube