Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

C# 13

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

    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

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    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

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.