Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C#
  4. Understanding Records and Pattern Matching in C# 9 and Above

Understanding Records and Pattern Matching in C# 9 and Above

Date- Mar 16,2026 56
c# records

Overview of Records and Pattern Matching

Records are a new reference type in C# that provide built-in functionality for encapsulating data. They are particularly useful for immutable data models, making them ideal for scenarios like data transfer objects or data structures in functional programming. Pattern Matching enhances the ability to check the shape of data and destructure complex types in a more readable way. Together, these features simplify code and improve maintainability.

Prerequisites

  • Basic knowledge of C# and object-oriented programming concepts
  • Familiarity with C# 8 or earlier versions
  • Understanding of data structures and immutability
  • Visual Studio or any C# compatible IDE installed

What are Records?

Records in C# are a special kind of class designed to hold data. They automatically provide implementations for Equals, GetHashCode, and ToString methods, making them ideal for data-centric applications.

public record Person(string FirstName, string LastName);

public class Program
{
    public static void Main(string[] args)
    {
        var person1 = new Person("John", "Doe");
        var person2 = new Person("John", "Doe");

        Console.WriteLine(person1); // Outputs: Person { FirstName = John, LastName = Doe }
        Console.WriteLine(person1 == person2); // Outputs: True
    }
}

In this code example:

  • We define a record named Person with two properties: FirstName and LastName.
  • In the Main method, we create two instances of Person, person1 and person2, with the same values.
  • When we print person1, it uses the autogenerated ToString method to display its properties.
  • We compare person1 and person2 using the equality operator, which returns true because records implement value-based equality.

Immutable Data with Records

One of the most significant advantages of records is that they are immutable by default. This means that once you create an instance of a record, its state cannot be changed. This immutability leads to safer and more predictable code.

public record Address(string Street, string City);

public class Program
{
    public static void Main(string[] args)
    {
        var address = new Address("123 Main St", "New York");
        // address.Street = "456 Elm St"; // This line would cause a compile-time error

        var newAddress = address with { Street = "456 Elm St" };
        Console.WriteLine(newAddress); // Outputs: Address { Street = 456 Elm St, City = New York }
    }
}

In this example:

  • We define a record named Address with two properties: Street and City.
  • We create an instance of Address named address.
  • A comment indicates that trying to change address.Street directly would result in a compile-time error due to immutability.
  • We use the with expression to create a new instance newAddress based on address, changing only the Street property.

Pattern Matching Basics

Pattern matching allows you to check the shape of objects and destructure them in a clear and concise way. It is especially useful in scenarios involving conditional logic.

public class Shape
{
    public record Circle(double Radius);
    public record Square(double Side);

    public static void PrintShapeInfo(object shape)
    {
        switch (shape)
        {
            case Circle circle:
                Console.WriteLine($"Circle with radius: {circle.Radius}");
                break;
            case Square square:
                Console.WriteLine($"Square with side length: {square.Side}");
                break;
            default:
                Console.WriteLine("Unknown shape");
                break;
        }
    }

    public static void Main(string[] args)
    {
        var circle = new Circle(5.0);
        var square = new Square(4.0);

        PrintShapeInfo(circle); // Outputs: Circle with radius: 5
        PrintShapeInfo(square); // Outputs: Square with side length: 4
    }
}

In this code:

  • We define a class Shape that contains two records: Circle and Square.
  • The PrintShapeInfo method uses a switch statement to perform pattern matching on the shape object.
  • For each case, we can access the properties of the matched shape type directly, allowing for clean and readable code.

Advanced Pattern Matching Features

C# 9 introduced advanced pattern matching features, including relational patterns and logical patterns, which allow for more complex conditional checks.

public class Temperature
{
    public double Value { get; set; }
    public string Scale { get; set; }
}

public class Program
{
    public static void DescribeTemperature(Temperature temp)
    {
        switch (temp)
        {
            case { Value: > 30, Scale: "Celsius" }:
                Console.WriteLine("It's a hot day!");
                break;
            case { Value: < 0, Scale: "Celsius" }:
                Console.WriteLine("It's freezing!");
                break;
            case { Value: var value, Scale: "Fahrenheit" } when value > 86:
                Console.WriteLine("It's a hot day in Fahrenheit!");
                break;
            default:
                Console.WriteLine("Temperature is normal.");
                break;
        }
    }

    public static void Main(string[] args)
    {
        var temp1 = new Temperature { Value = 35, Scale = "Celsius" };
        var temp2 = new Temperature { Value = -5, Scale = "Celsius" };
        var temp3 = new Temperature { Value = 90, Scale = "Fahrenheit" };

        DescribeTemperature(temp1); // Outputs: It's a hot day!
        DescribeTemperature(temp2); // Outputs: It's freezing!
        DescribeTemperature(temp3); // Outputs: It's a hot day in Fahrenheit!
    }
}

In this code:

  • We create a class Temperature with properties Value and Scale.
  • The DescribeTemperature method uses pattern matching to check the temperature's value and scale.
  • We use relational patterns (e.g., Value: > 30) and logical patterns (e.g., when) to create sophisticated conditions.

Best Practices and Common Mistakes

Best Practices:

  • Use records for data structures that require value equality.
  • Make use of the with expression for immutability and clarity.
  • Leverage pattern matching to make your code more expressive and easier to understand.

Common Mistakes:

  • Forgetting that records are immutable by default, which can lead to confusion when trying to modify properties.
  • Using pattern matching without considering all possible cases, which can lead to unhandled scenarios.
  • Overusing pattern matching for simple checks where traditional conditional statements would suffice.

Conclusion

Records and pattern matching in C# 9 and above provide powerful tools for building clean, maintainable, and expressive code. By understanding how to effectively use records for data encapsulation and pattern matching for conditional logic, developers can enhance their C# programming skills significantly.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Leveraging New .NET 10 Features for Modern Applications
Mar 19, 2026
Understanding Extension Methods in C#: Enhancing Your Code with Ease
Mar 16, 2026
Understanding Collections in C#: List, Dictionary, Queue, and Stack
Mar 16, 2026
Understanding Generics in C#: A Comprehensive Guide
Mar 16, 2026
Previous in C#
Understanding Extension Methods in C#: Enhancing Your Code with E…
Next in C#
Understanding Memory Management and Garbage Collection in .NET
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your C# interview with curated Q&As for all levels.

View C# Interview Q&As

More in C#

  • Zoom C# Wrapper Integration 12905 views
  • Convert HTML String To Image In C# 11504 views
  • The report definition is not valid or is not supported by th… 10856 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9843 views
  • Get IP address using c# 8688 views
View all C# posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor