Skip to content

FR CSharp Keyword closed

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: closed

Manuel C# · Guide pratique

Modificateur d'aperçu de hiérarchie fermée : les descendants immédiats doivent appartenir au même assemblage. Le compilateur peut analyser l'intégralité d'un changement par de tels descendants.

Exemple simple (Example.cs)

Enregistrez cet exemple séparément dans Example.cs. Exécutez-le indépendamment des autres exemples de cette page.

Example.cs:

// PROJECT-EXAMPLE
sealed class Finished { }

Ce petit exemple utilise C# 14. Il ne réalise pas la fonctionnalité en préversion closed, indisponible ici.

Syntaxe

public closed class Base { ... }

L'annuaire officiel fait référence au fermé C# 15. Le C# 14 / .NET 10 actuel n'accepte pas ces exemples : un compilateur de prévisualisation compatible et une plate-forme d'exécution correspondante sont requis. Il s'agit d'un aperçu d'une future fonctionnalité, et non d'une commande d'activation de support.

Statut : aperçu ; la configuration actuelle C# 14 / .NET 10 ne prend pas en charge ces exemples.

Exemple 1 : formulaire de 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;

Exemple 2 : une autre forme pratique

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;

Source officielle : Microsoft Apprendre.

Clone this wiki locally