.NETヘルプ C# Sealed Class(開発者向けの動作方法) Curtis Chau 更新日:6月 22, 2025 Download IronPDF NuGet Download テキストの検索と置換 テキストと画像のスタンプ Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article オブジェクト指向プログラミングの世界では、C# 言語は開発者に汎用性のあるツールセットを提供し、堅牢なソフトウェアを設計し実装することができます。 クラス継承に追加の制御層を加える機能のひとつは、sealed クラスの概念です。 Sealed クラスは、継承階層を制限する独自の方法を提供し、特定のシナリオで有益なセキュリティとカプセル化のレベルを提供します。 In this article, we will delve into the intricacies of the C# sealed classes and also explore the IronPDF NuGet package from Iron Software. Sealed クラスと Sealed メソッドとは何ですか? Sealed クラス: C# では、sealed クラスは継承できないクラスです。 sealed キーワードを使用することで、開発者は他のクラスが sealed クラスから派生または拡張するのを防止できます。 この意図的な制限により、sealed クラスが他のクラスの基底クラスとして使用されることを防ぎ、継承階層の範囲を制限します。 Sealed クラスは、開発者がクラスの構造を制御し確定したい場合によく使用されます。 次の例を考えてみましょう。 public sealed class Animal { public string Species { get; set; } public void MakeSound() { Console.WriteLine("Generic animal sound"); } } // The following code will cause a compilation error because 'Animal' is sealed and cannot be inherited: // public class Dog : Animal // Error: Cannot inherit from sealed class 'Animal' // { // } public sealed class Animal { public string Species { get; set; } public void MakeSound() { Console.WriteLine("Generic animal sound"); } } // The following code will cause a compilation error because 'Animal' is sealed and cannot be inherited: // public class Dog : Animal // Error: Cannot inherit from sealed class 'Animal' // { // } Public NotInheritable Class Animal Public Property Species() As String Public Sub MakeSound() Console.WriteLine("Generic animal sound") End Sub End Class ' The following code will cause a compilation error because 'Animal' is sealed and cannot be inherited: ' public class Dog : Animal // Error: Cannot inherit from sealed class 'Animal' ' { ' } $vbLabelText $csharpLabel Struct とは異なり、暗黙的に sealed されているわけではなく、上記のように sealed キーワードを使用して宣言する必要があります。 この例では、Animal クラスが sealed として宣言されており、他のクラスがそれを継承することは不可能です。 Sealed メソッド: クラス全体をシールすることに加えて、C# ではクラス内の個々のメソッドをシールすることもできます。 Sealed メソッドは、派生クラスによってオーバーライドできないメソッドです。これにより、アプリケーションの論理の中でメソッドの動作がすべてのサブクラス間で一貫していることが保証されます。 メソッドをシールするには、sealed 修飾子を使用します。 public class Animal { public string Species { get; set; } // A virtual method allows derived classes to override it. public virtual void MakeSound() { Console.WriteLine("Generic animal sound"); } } public class Dog : Animal { // The sealed override prevents further overriding of this method. public sealed override void MakeSound() { Console.WriteLine("Bark!"); } } public class Animal { public string Species { get; set; } // A virtual method allows derived classes to override it. public virtual void MakeSound() { Console.WriteLine("Generic animal sound"); } } public class Dog : Animal { // The sealed override prevents further overriding of this method. public sealed override void MakeSound() { Console.WriteLine("Bark!"); } } Public Class Animal Public Property Species() As String ' A virtual method allows derived classes to override it. Public Overridable Sub MakeSound() Console.WriteLine("Generic animal sound") End Sub End Class Public Class Dog Inherits Animal ' The sealed override prevents further overriding of this method. Public NotOverridable Overrides Sub MakeSound() Console.WriteLine("Bark!") End Sub End Class $vbLabelText $csharpLabel virtual キーワードは派生クラスでメソッドをオーバーライドすることを許可し、sealed キーワードは、基底クラスの仮想メソッドを以降のすべてのサブクラスでさらにオーバーライドするのを防ぎます。 Sealed クラスとクラス メンバー: Sealed クラスは、プロパティ、メソッド、およびイベントなどの sealed メンバーを含めることもできます。 この sealed クラスと sealed メンバーの組み合わせにより、クラスの動作と構造に対するコントロールの高いレベルが確立されます。 次の例を考えてみましょう。 public sealed class ControlledClass { // A sealed property that prevents overriding. public sealed string ControlledProperty { get; set; } // A method that cannot be redefined by derived classes. public virtual sealed void ControlledMethod() { // Method implementation Console.WriteLine("Executing controlled method."); } // A sealed event that cannot be subscribed to or raised by derived classes. public sealed event EventHandler ControlledEvent; // Sealed indexers, if applicable public sealed string this[int index] { get { return "Value"; } set { // Setter implementation } } } public sealed class ControlledClass { // A sealed property that prevents overriding. public sealed string ControlledProperty { get; set; } // A method that cannot be redefined by derived classes. public virtual sealed void ControlledMethod() { // Method implementation Console.WriteLine("Executing controlled method."); } // A sealed event that cannot be subscribed to or raised by derived classes. public sealed event EventHandler ControlledEvent; // Sealed indexers, if applicable public sealed string this[int index] { get { return "Value"; } set { // Setter implementation } } } Public NotInheritable Class ControlledClass ' A sealed property that prevents overriding. Public NotOverridable Property ControlledProperty() As String ' A method that cannot be redefined by derived classes. Public Overridable NotOverridable Sub ControlledMethod() ' Method implementation Console.WriteLine("Executing controlled method.") End Sub ' A sealed event that cannot be subscribed to or raised by derived classes. Public Event ControlledEvent As EventHandler ' Sealed indexers, if applicable Default Public NotOverridable Property Item(ByVal index As Integer) As String Get Return "Value" End Get Set(ByVal value As String) ' Setter implementation End Set End Property End Class $vbLabelText $csharpLabel この例では、 ControlledClass のあらゆる側面がシールされており、プロパティ、メソッド、イベント、そして該当する場合はインデクサーも含まれます。 このシールレベルは強固で改変不可能な構造を提供し、クラスの設計が固定されるべきシナリオに理想的です。 シールド クラスの背後にある理由 コード セキュリティ: Sealed クラスは、無許可のアクセスや変更を防ぐことでコード セキュリティに貢献します。 クラスがシールされていると、明確なインターフェイスと動作を持つクローズド エンティティとして機能します。 このカプセル化により、コードベースの安定性やセキュリティを潜在的に損なう可能性のある意図しない副作用や変更のリスクが最小限に抑えられます。 設計の一貫性: 大規模なコードベースやフレームワークでは、設計の一貫性の維持が非常に重要です。 シールされたクラスは、固定された構造を持つ基礎となるビルディング ブロックとして機能し、意図しない変更の可能性を減らします。 これは、クラスがシステムのコアコンポーネントとして機能し、その動作が異なるモジュール間で一貫しているべきであるシナリオに特に有益です。 シールド クラスの使用に関するベスト プラクティス シールされたクラスを控えめに使用する: シールされたクラスは利点を提供しますが、慎重に使用する必要があります。 Sealed クラスの過剰使用は、固定されたコードになる可能性があります。 意図を文書化する: クラスまたはメソッドをシールする際は、この決定の背後にある意図を文書化することが重要です。 特定のクラスがシールされている理由や、その選択につながった設計上の考慮事項を説明します。 将来の拡張性を考慮する: クラスをシールする前に、将来の要件が拡張性を必要とする可能性を考慮してください。 クラスが拡張される必要がある可能性がある場合、シールすることは将来の開発を妨げる可能性があります。 安定性のためにシールされたメソッドを使用する: メソッドのコアの動作を異なるサブクラスで安定させる必要がある場合、メソッドをシールすることは有益です。 これにより、コードの予測可能性が向上します。 抽象化できない: Sealed クラス/メソッドは、抽象クラス/メソッドとして抽象化することはできません。抽象クラスは他のクラスによって継承されるように設計されていますが、シールされたクラスは継承を制限します。 IronPDFの紹介 IronPDF is a C# PDF library from Iron Software の C# PDF ライブラリであり、最新の PDF ジェネレーターおよびリーダーです。 インストール IronPDF は、NuGet パッケージ マネージャー コンソールまたは Visual Studio パッケージ マネージャーを使用してインストールできます。 以下はコンソール用のコマンドです: Install-Package IronPdf または、NuGet パッケージ マネージャーを使用して IronPDF をインストールするには、NuGet パッケージ マネージャーの検索バーに「ironpdf」と入力します。 IronPDF とシールドされたクラス Sealed キーワードと IronPDF は連携して、サブクラス ライブラリまたは派生ライブラリが継承メンバーをオーバーライドできないようにし、PDF を生成することもできます。 namespace OrderBy { public class Program { static void Main() { Console.WriteLine("Demo Sealed Class and IronPdf"); var dog = new Dog(); dog.MakeSound(); dog.Print(); } } // Base class public class Animal { public string Species { get; set; } public virtual void MakeSound() { Console.WriteLine("Generic animal sound"); } public virtual void Print() { Console.WriteLine("Generic animal Print"); } } public class Dog : Animal { // Sealed override ensures method cannot be overridden in further derived classes. public sealed override void MakeSound() { Console.WriteLine("Bark!"); } public sealed override void Print() { var pdfRenderer = new ChromePdfRenderer(); string content = @" <!DOCTYPE html> <html> <body> <h1>Hello, Dog!</h1> <p>This is Print from Derived class.</p> <p>Print Animal Dog</p> <p>Print Animal Sound: Bark</p> </body> </html>"; pdfRenderer.RenderHtmlAsPdf(content).SaveAs("dog.pdf"); } } } namespace OrderBy { public class Program { static void Main() { Console.WriteLine("Demo Sealed Class and IronPdf"); var dog = new Dog(); dog.MakeSound(); dog.Print(); } } // Base class public class Animal { public string Species { get; set; } public virtual void MakeSound() { Console.WriteLine("Generic animal sound"); } public virtual void Print() { Console.WriteLine("Generic animal Print"); } } public class Dog : Animal { // Sealed override ensures method cannot be overridden in further derived classes. public sealed override void MakeSound() { Console.WriteLine("Bark!"); } public sealed override void Print() { var pdfRenderer = new ChromePdfRenderer(); string content = @" <!DOCTYPE html> <html> <body> <h1>Hello, Dog!</h1> <p>This is Print from Derived class.</p> <p>Print Animal Dog</p> <p>Print Animal Sound: Bark</p> </body> </html>"; pdfRenderer.RenderHtmlAsPdf(content).SaveAs("dog.pdf"); } } } Namespace OrderBy Public Class Program Shared Sub Main() Console.WriteLine("Demo Sealed Class and IronPdf") Dim dog As New Dog() dog.MakeSound() dog.Print() End Sub End Class ' Base class Public Class Animal Public Property Species() As String Public Overridable Sub MakeSound() Console.WriteLine("Generic animal sound") End Sub Public Overridable Sub Print() Console.WriteLine("Generic animal Print") End Sub End Class Public Class Dog Inherits Animal ' Sealed override ensures method cannot be overridden in further derived classes. Public NotOverridable Overrides Sub MakeSound() Console.WriteLine("Bark!") End Sub Public NotOverridable Overrides Sub Print() Dim pdfRenderer = New ChromePdfRenderer() Dim content As String = " <!DOCTYPE html> <html> <body> <h1>Hello, Dog!</h1> <p>This is Print from Derived class.</p> <p>Print Animal Dog</p> <p>Print Animal Sound: Bark</p> </body> </html>" pdfRenderer.RenderHtmlAsPdf(content).SaveAs("dog.pdf") End Sub End Class End Namespace $vbLabelText $csharpLabel 以下は IronPDF から生成された PDF です。 ライセンス (無料トライアル利用可能) IronPDF. このキーはappsettings.jsonに配置する必要があります。 { "IronPdf.LicenseKey": "your license key" } トライアルライセンスを取得するには、メールアドレスを提供してください。 結論 C# sealed クラスは、開発者に継承階層を制御し、特定のクラスやそのメンバーを拡張やオーバーライド不可能にする強力なメカニズムを提供します。 Sealed クラスの使用は慎重に考慮されるべきですが、機能をカプセル化し、意図しない変更を防ぐ効果的な手段を提供します。 Sealed クラスとメソッドの概念を理解することで、開発者はこの制限を適用すべき状況を判断し、メンテナンス性、セキュリティ性、一貫性のあるソフトウェア システムの実現に寄与することができます。 また、IronPDF と共に PDF ドキュメントを印刷することもできます。 よくある質問 C#におけるシールドクラスはどのように機能しますか? C#では、シールドクラスはsealedキーワードを使用して定義されます。これにより、他のクラスがそれを継承することができず、クラスの実装が変更されないことが保証されます。 なぜ開発者はC#でシールドクラスを使用すべきですか? シールドクラスは継承を防ぐことでコードの整合性を維持するために使用されます。これにより、大規模なシステムで設計の整合性が重要な場合に、クラスの動作が一貫して安全であることが保証されます。 シールドクラスは、シールドされたメソッドを持つことができますか? はい、シールドクラスには、シールドされたメソッドを含めることができます。これにより、メソッドは派生クラスでオーバーライドができなくなり、クラスの機能の安全性と一貫性がさらに向上します。 クラスでシールドメソッドを使用する利点は何ですか? シールドメソッドは派生クラスによるオーバーライドを防止するため、メソッドの元の動作を維持し、一貫性を保ち予期しない修正から保護します。 シールドクラスを使用する状況の例を提供できますか? シールドクラスは、継承を通じた変更を防ぐためにクラスの実装を固定したいときに役立ちます。ユーティリティクラスや安定性が求められる機密操作に取り組む際に使用します。 シールドクラスはC#におけるPDF生成とどのように関連していますか? IronPDFのようなPDFライブラリを使用すると、シールドクラスを活用して、継承による変更を防止することでPDF生成プロセスの一貫性と安全性を確保できます。 IronPDFのようなサードパーティライブラリとシールドクラスを使用することは可能ですか? はい、シールドクラスはIronPDFのようなサードパーティライブラリと一緒に使用して、PDF生成ロジックを安全で継承されないクラス構造にカプセル化できます。 NuGetを使用してC# PDFライブラリをインストールするにはどうすればよいですか? IronPDFのようなC# PDFライブラリを、dotnet add package IronPdfコマンドを使用するか、Visual Studio NuGetパッケージマネージャで「ironpdf」を検索してインストールできます。 ソフトウェア設計でシールドクラスを使用する際の考慮事項は何ですか? 開発者は将来の拡張性の必要性を考慮し、クラスをシールドする理由を文書化すべきです。シールドクラスを使用することでセキュリティと保守性が向上しますが、アプリケーションが求める柔軟性とのバランスが必要です。 C#開発者に推奨されるPDFジェネレーターは何ですか? IronPDFは、C#開発者に推奨されるPDFジェネレーターであり、NuGetパッケージとして堅牢なPDF作成および操作機能を提供します。 Curtis Chau 今すぐエンジニアリングチームとチャット テクニカルライター Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。 関連する記事 更新日 9月 4, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む 更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む 更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む C# Priority Queue(開発者向けの動作方法)C# LINQ Join Query Syntax(開発...
更新日 9月 4, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む
更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む
更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む