Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In the world of object-oriented programming, the C# language provides developers with a versatile set of tools to design and implement robust software. One such feature that adds an extra layer of control to class inheritance is the concept of a sealed class. Sealed classes offer a unique way to restrict the inheritance hierarchy, providing a level of security and encapsulation that can be beneficial in certain scenarios. 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 Class: In C#, a sealed class is a class that cannot be inherited. By using the sealed keyword, developers can prevent other classes from deriving or extending from the sealed class. This deliberate restriction ensures that the sealed class cannot be used as a base class for any other class, limiting the scope of the inheritance hierarchy. Sealed classes are often employed when a developer wants to control and finalize the structure of a class, preventing unintended modifications through inheritance.
Consider the following example:
public sealed class Animal
{
public string Species { get; set; }
public void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
public class Dog : Animal // Error: Cannot be inherited
{
}
public sealed class Animal
{
public string Species { get; set; }
public void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
public class Dog : Animal // Error: Cannot be inherited
{
}
Public NotInheritable Class Animal
Public Property Species() As String
Public Sub MakeSound()
Console.WriteLine("Generic animal sound")
End Sub
End Class
Public Class Dog ' Error: Cannot be inherited
Inherits Animal
End Class
Unlike structs which are implicitly sealed, the sealed class has to be declared using the sealed keyword as shown above. In this example, the Animal Class is declared as sealed, making it impossible for any other class to inherit from it.
Sealed Method: In addition to sealing entire classes, C# also allows developers to seal individual methods within a class. A sealed method is a method that any derived class cannot override. This ensures that the behavior of the method remains consistent across all subclasses, providing a level of predictability in the application's logic.
To seal a method, use the sealed modifier:
public class Animal
{
public string Species { get; set; }
public virtual void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
public class Dog : Animal
{
public sealed override void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
public class Animal
{
public string Species { get; set; }
public virtual void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
public class Dog : Animal
{
public sealed override void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
Public Class Animal
Public Property Species() As String
Public Overridable Sub MakeSound()
Console.WriteLine("Generic animal sound")
End Sub
End Class
Public Class Dog
Inherits Animal
Public NotOverridable Overrides Sub MakeSound()
Console.WriteLine("Generic animal sound")
End Sub
End Class
The virtual keyword allows the method to be overridden in derived classes, while the sealed keyword prevents further overriding the base class virtual method in any subsequent subclasses.
Sealed Class and Class Members: Sealed classes can also include sealed members, such as properties, methods, and events. This combination of sealed class and sealed members ensures a high degree of control over the class's behavior and structure.
Consider the following example:
public sealed class ControlledClass
{
public sealed string ControlledProperty { get; set; }
public virtual sealed void ControlledMethod()
{
// Method implementation
}
// Sealed event
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
{
public sealed string ControlledProperty { get; set; }
public virtual sealed void ControlledMethod()
{
// Method implementation
}
// Sealed event
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
Public NotOverridable Property ControlledProperty() As String
Public Overridable NotOverridable Sub ControlledMethod()
' Method implementation
End Sub
' Sealed event
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)
End Set
End Property
End Class
In this example, every aspect of the ControlledClass
is sealed – the property, method, event, and even an indexer if applicable. This level of sealing provides a robust and unalterable structure, ideal for scenarios where the class's design should remain fixed.
Code Security: Sealed classes contribute to code security by preventing unauthorized access and modification. When a class is sealed, it serves as a closed entity with a well-defined interface and behavior. This encapsulation minimizes the risk of unintended side effects or alterations that could potentially compromise the stability and security of the codebase.
Design Integrity: In larger codebases or frameworks, maintaining design integrity is paramount. A sealed class acts as a foundational building block with fixed structures, reducing the chances of unintended modifications. This is particularly beneficial in scenarios where a class serves as a core component of a system, and its behavior should remain consistent across different modules.
classesrestrict
inheritance.IronPDF is a C# PDF library from Iron Software and is a modern PDF generator and reader.
IronPDF can be installed using the NuGet Package Manager console or using the Visual Studio package manager. Below is the command for the console:
dotnet add package IronPdf
Or, to install IronPDF using NuGet Package Manager, search "ironpdf" in the search bar of NuGet Package Manager.
Together the sealed keyword and IronPDF can be used to prevent a sub-class library or a derived library from overriding inherited members and also generate PDFs.
namespace OrderBy;
public class Program
{
static void Main()
{
Console.WriteLine("Demo Sealed Class and IronPdf");
var dog = new Dog();
dog.MakeSound();
dog.Print();
}
}
// Sealed 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
{
public sealed override void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
public sealed override void Print()
{
var pdfDocument = 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>";
pdfDocument.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();
}
}
// Sealed 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
{
public sealed override void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
public sealed override void Print()
{
var pdfDocument = 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>";
pdfDocument.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
' Sealed 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
Public NotOverridable Overrides Sub MakeSound()
Console.WriteLine("Generic animal sound")
End Sub
Public NotOverridable Overrides Sub Print()
Dim pdfDocument = 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>"
ignore ignore ignore ignore ignore ignore ignore ignore pdfDocument.RenderHtmlAsPdf(content).SaveAs("dog.pdf")
End Sub
End Class
End Namespace
Below is the PDF generated from IronPDF
IronPDF. This key needs to be placed in appsettings.json.
"IronPdf.LicenseKey": "your license key"
"IronPdf.LicenseKey": "your license key"
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'"IronPdf.LicenseKey": "your license key"
Provide your email to get a trial license.
C# sealed classes offer developers a powerful mechanism to control the inheritance hierarchy and ensure that certain classes and their members cannot be extended or overridden. While the use of sealed classes should be carefully considered, they provide an effective means of encapsulating functionality and preventing unintended modifications. By understanding the concept of sealed classes and methods, developers can make informed decisions about when and where to apply this restriction, contributing to creating maintainable, secure, and predictable software systems. Together with IronPDF, we can also print PDF documents.
9 .NET API products for your office documents