Skip to content

IT CSharp Keyword closed

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: closed

Manuale C# · Guida pratica

Modificatore anteprima gerarchia chiusa: i discendenti immediati devono appartenere allo stesso assembly. Il compilatore può analizzare la completezza di un passaggio da parte di tali discendenti.

Esempio semplice (Example.cs)

Salvare questo esempio separatamente come Example.cs. Eseguirlo indipendentemente dagli altri esempi di questa pagina.

Example.cs:

// PROJECT-EXAMPLE
sealed class Finished { }

Questo breve esempio usa C# 14. Non implementa il costrutto in anteprima closed, non disponibile qui.

Sintassi

public closed class Base { ... }

La directory ufficiale si riferisce chiusa a C# 15. L'attuale C# 14 / .NET 10 non accetta questi esempi: sono richiesti un compilatore di anteprima compatibile e una piattaforma runtime corrispondente. Questa è una panoramica di una funzionalità futura, non un comando di abilitazione del supporto.

Stato: anteprima; la configurazione corrente C# 14 / .NET 10 non supporta questi esempi.

Esempio 1: forma base

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;

Esempio 2: un'altra forma pratica

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;

Fonte ufficiale: Microsoft Impara.

Clone this wiki locally