Skip to content

ES CSharp Keyword closed

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: closed

Manual C# · Guía Práctica

Modificador de vista previa de jerarquía cerrada: los descendientes inmediatos deben pertenecer al mismo conjunto. El compilador puede analizar la integridad de un cambio realizado por dichos descendientes.

Ejemplo sencillo (Example.cs)

Guarde este ejemplo por separado como Example.cs. Ejecútelo independientemente de los demás ejemplos de esta página.

Example.cs:

// PROJECT-EXAMPLE
sealed class Finished { }

Este ejemplo breve usa C# 14. No implementa la construcción en versión preliminar closed, que no está disponible aquí.

Sintaxis

public closed class Base { ... }

El directorio oficial se refiere cerrado a C# 15. El C# 14 / .NET 10 actual no acepta estos ejemplos: se requiere un compilador de vista previa compatible y una plataforma de ejecución correspondiente. Esta es una descripción general de una característica futura, no un comando de habilitación de soporte.

Estado: vista previa; la configuración actual C# 14 / .NET 10 no admite estos ejemplos.

Ejemplo 1: forma básica

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;

Ejemplo 2: otra forma práctica

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;

Fuente oficial: Microsoft Aprender.

Clone this wiki locally