Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

C# 13

April 16, 2025
Tom Deseyn
Related topics:
.NET
Related products:
Developer Tools

Share:

    Discover the C# 13 new features in this two-part series. C# 13 is supported by the .NET 9 SDK, which was released in November 2024. The features described in this article are things you’ll find in everyday programs. In the second article, we’ll cover more specialized features.

    Params collections

    The C# params modifier enables a method to accept an arbitrary number of arguments of a certain type. For example, this code

    Console.WriteLine("{0} + {1} + {2} = {3}", i, j, k, sum);

    calls the following WriteLine params overload:

    static void WriteLine (string format, params object?[]? arg);

    The params argument must be the last argument of the method. Previously the compiler required it to be an array type. With C# 13, the compiler allows collection types. This means the type can be a Span<T>, ReadOnlySpan<T>, IEnumerable<T>, (IReadOnly)Collection<T>, (IReadOnly)List<T>, or any type that implements IEnumerable<T> and has an Add method that accepts the item type. The compiler generates optimized code based on the provided arguments and the params target type.

    .NET 9 leverages params collections. For example, Console.WriteLine has a new overload that accepts the arg as a ReadOnlySpan:

    static void WriteLine (string format, scoped ReadOnlySpan<object?> arg);

    For our initial example, the compiler calls this new overload and rather than allocating an array (on the heap), it will construct the ReadOnlySpan on the stack instead.

    Partial properties and indexers

    C# 13 adds support for partial properties and indexers. Like C# 9’s partial methods, this allows to split the declaration and implementation of the property (or indexer) in separate files.

    The main use-case for this is to let the user define a property and have a source-generator provide the implementation.

    When the partial keyword is used, lack of a body is treated as the property declaration. To implement the property, the accessor body must be explicitly included.

    partial class C
    {
      // Declare property of type 'string' with a getter.
      public partial string Property { get; }
    }
    
    // -- Other file, probably generated by a source generator.
    partial class C
    {
      private string _value;
    
      // Provide implementation of the property.
      public partial string Property { get => _value; }
    }

    Implicit index access in object initializers

    C# 8 introduced support for ranges and indexes. This enables us to index elements from the end of a collection using the ^ operator. This operator can now also be used in object initializers, as shown in the next example.

    var buffer = new Buffer()
    {
      Data =
      {
        [^1] = 10, // Initialize the last element to '10'.
      }
    };
    
    sealed class Buffer
    {
      public byte[] Data { get; } = new byte[256];
    }

    Note that in the object initializer, the Data = does not represent an assignment of the property. It initializes the value returned by the getter using the initializer block.

    ref and unsafe in iterators and async methods

    C#’s support for async methods and implementing iterators using the yield keyword rely on the compiler generating state machines that run through different parts of the user’s code based on the current state of that state machine. Consequently, the user code isn’t running once as a single function on the call stack, but it runs as part of separate function calls.

    C#’s support for unsafe and ref features depend on code blocks that run as in a single call stack function. For example: a ref struct is a structure that is stored on the stack and therefore the stack can not change while a ref struct is used.

    To enforce this, the C# compiler disallowed the use of unsafe and ref in async methods and iterator implementations. To use these features from an async/iterator method, we need to call into separate non-async/iterator methods.

    With C# 13, the compiler is now allowing the use of unsafe and ref in iterators and async methods as long as the code section they are used in will be part of the same stack function. For async methods, this means we can’t cross an async keyword; and for iterator methods, it means we can’t cross a yield.

    The following example shows the initializing of a ReadOnlySpan (which is a ref struct) in an async method. This code is disallowed in previous C# versions.

    int bytesRead = await stream.ReadAsync(buffer);
    ReadOnlySpan<byte> received = buffer.AsSpan(0, bytesRead);
    if (received.Length ...

    What's next?

    In this article, we looked at params collections, which extend the usage of C#’s params keyword. We also discussed partial properties and indexers and how these can be used by a source generator. Finally, we covered the use of ref and unsafe in async methods and iterator methods, and the use of the ^ operator in object initializers. In both cases, the compiler allows using these features in more places.

    In the next article, we’ll look at the C# 13 new advanced features.

    Last updated: April 23, 2025

    Related Posts

    • Some more C# 13

    • C# 12: Collection expressions and primary constructors

    • C# 11: Pattern matching and static abstract interfaces

    • Three ways to containerize .NET applications on Red Hat OpenShift

    Recent Posts

    • Cloud bursting with confidential containers on OpenShift

    • Reach native speed with MacOS llama.cpp container inference

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    What’s up next?

    Get a preview of the Red Hat Certified Engineer Ansible Automation Study Guide (O’Reilly), which covers key Ansible concepts for your system administration needs.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue