Skip to content

EN CSharp Keyword closed

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

C#: closed

Handbook C# · Practical Guide

Closed Hierarchy Preview Modifier: Immediate descendants must belong to the same assembly. The compiler can analyze the completeness of a switch by such descendants.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
sealed class Finished { }

This short example uses C# 14. It does not implement the preview construct closed, which is unavailable here.

Syntax

public closed class Base { ... }

The official directory refers closed to C# 15. The current C# 14 / .NET 10 does not accept these examples: a compatible preview compiler and a corresponding runtime platform are required. This is an overview of a future feature, not a support enablement command.

Status: preview; the current configuration C# 14 / .NET 10 does not support these examples.

Example 1: simple form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Status status = new Ready();
        Console.WriteLine(status switch { Ready => "ready", Busy => "busy" });
    }
}
public closed record Status;
public sealed record Ready : Status;
public sealed record Busy : Status;

Example 2: another practical form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Shape shape = new Circle(3);
        Console.WriteLine(shape switch { Circle(var radius) => radius * radius, Box(var width) => width * width });
    }
}
public closed record Shape;
public sealed record Circle(int Radius) : Shape;
public sealed record Box(int Width) : Shape;

Official source: Microsoft Learn.

Clone this wiki locally