Skip to content

JA CSharp Keyword closed

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: closed

ディレクトリ C# · 実践的な指導

閉じた階層プレビュー修飾子: 直接の子孫は同じアセンブリに属している必要があります。コンパイラは、そのような子孫によるスイッチの完全性を分析できます。

簡単な例 (Example.cs)

この例を Example.cs として別に保存し、このページの他の例とは独立して実行してください。

Example.cs:

// PROJECT-EXAMPLE
sealed class Finished { }

この短い例は C# 14 を使用します。ここでは非対応のプレビュー構文 closed は実装していません。

構文

public closed class Base { ... }

公式ディレクトリは、C# 15 を指します。現在の C# 14 / .NET 10 はこれらの例を受け入れません。互換性のあるプレビュー コンパイラーと対応するランタイム プラットフォームが必要です。これは将来の機能の概要であり、サポートを有効にするコマンドではありません。

ステータス: プレビュー。現在の構成 C# 14 / .NET 10 は、これらの例をサポートしていません。

例 1: 基本形式

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;

例 2: 別の実用的な形式

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;

公式ソース: Microsoft Learn。

Clone this wiki locally