Saltar al pie de página
.NET AYUDA

C# Clase Sellada (Cómo Funciona para Desarrolladores)

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.

What are Sealed Classes and Sealed Methods?

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");
    }
}

// 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

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; }

    // 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

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
{
    // 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

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.

The Rationale Behind Sealed Class

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.

Best Practices for Using Sealed Class

  1. Use the Sealed Class Sparingly: While the sealed class offers benefits, they should be employed judiciously. Overuse of sealed classes can lead to rigid and less maintainable code.
  2. Document Intent: When sealing a class or method, it's crucial to document the intent behind this decision. Explain why a particular class is sealed and what design considerations led to that choice.
  3. Consider Future Extensibility: Before sealing a class, consider whether future requirements might necessitate extensibility. If there's a likelihood that a class will need to be extended, sealing it may hinder future development.
  4. Use Sealed Methods for Stability: Sealing methods can be beneficial when the core behavior of a method should remain stable across different subclasses. This can enhance the predictability of the code.
  5. It Cannot Also be Abstract: A sealed class/sealed methods cannot also be an abstract class/abstract methods as abstract classes are designed to be inherited by other classes while sealed classes restrict inheritance.

Introducing IronPDF

C# Sealed Class (How It Works For Developers): Figure 1 - IronPDF webpage

IronPDF is a C# PDF library from Iron Software and is a modern PDF generator and reader.

Installation

IronPDF can be installed using the NuGet Package Manager console or using the Visual Studio package manager. Below is the command for the console:

Install-Package IronPdf

Or, to install IronPDF using NuGet Package Manager, search "ironpdf" in the search bar of NuGet Package Manager.

C# Sealed Class (How It Works For Developers): Figure 2 - Installing the IronPDF library through the NuGet package manager

IronPDF and Sealed Classes

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();
        }
    }

    // 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

C# Sealed Class (How It Works For Developers): Figure 3 - Console output, showing execution command and any resulting console messages

Below is the PDF generated from IronPDF

C# Sealed Class (How It Works For Developers): Figure 4 - Outputted PDF generated with the previous code example

Licensing (Free Trial Available)

IronPDF. This key needs to be placed in appsettings.json.

{
    "IronPdf.LicenseKey": "your license key"
}

Provide your email to get a trial license.

Conclusion

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.

Preguntas Frecuentes

¿Cómo funciona una clase sellada en C#?

En C#, una clase sellada se define utilizando la palabra clave sealed. Esto impide que otras clases hereden de ella, asegurando que la implementación de la clase permanezca inalterada.

¿Por qué deberían los desarrolladores usar clases selladas en C#?

Las clases selladas se utilizan para mantener la integridad del código evitando la herencia. Esto asegura que el comportamiento de la clase permanezca consistente y seguro, especialmente en sistemas grandes donde la integridad del diseño es crucial.

¿Pueden las clases selladas tener métodos que también estén sellados?

Sí, las clases selladas pueden contener métodos que están a su vez sellados. Esto significa que los métodos no pueden ser sobrescritos en clases derivadas, aumentando aún más la seguridad y consistencia de la funcionalidad de la clase.

¿Cuál es el beneficio de usar métodos sellados en una clase?

Los métodos sellados previenen la sobrescritura por clases derivadas, lo que ayuda a mantener el comportamiento original del método, asegurando consistencia y protegiendo contra modificaciones no intencionadas.

¿Puede proporcionar ejemplos de cuándo usar clases selladas?

Las clases selladas son útiles cuando se quiere fijar la implementación de una clase para evitar cambios a través de la herencia, como en clases de utilidades o al trabajar con operaciones sensibles donde se requiere estabilidad.

¿Cómo se relacionan las clases selladas con la generación de PDF en C#?

Al usar una biblioteca de PDF como IronPDF, las clases selladas pueden aprovecharse para asegurar que los procesos de generación de PDF permanezcan consistentes y seguros, al prevenir modificaciones a través de la herencia.

¿Es posible usar clases selladas con bibliotecas de terceros como IronPDF?

Sí, las clases selladas pueden usarse junto con bibliotecas de terceros como IronPDF para encapsular la lógica de generación de PDF dentro de una estructura de clase segura y no heredable.

¿Cómo puedo instalar una biblioteca de PDF en C# usando NuGet?

Puede instalar una biblioteca de PDF en C#, como IronPDF, a través del Administrador de Paquetes NuGet usando el comando dotnet add package IronPdf o buscando 'ironpdf' en el Administrador de Paquetes NuGet de Visual Studio.

¿Cuáles son las consideraciones para usar clases selladas en el diseño de software?

Los desarrolladores deben considerar la necesidad de extensibilidad futura y documentar las razones para sellar una clase. Usar clases selladas puede mejorar la seguridad y mantenibilidad, pero debe equilibrarse con la flexibilidad requerida por la aplicación.

¿Cuál es un generador de PDF recomendado para desarrolladores C#?

IronPDF es un generador de PDF recomendado para desarrolladores C#, proporcionando capacidades robustas de creación y manipulación de PDF como un paquete de NuGet.

Curtis Chau
Escritor Técnico

Curtis Chau tiene una licenciatura en Ciencias de la Computación (Carleton University) y se especializa en el desarrollo front-end con experiencia en Node.js, TypeScript, JavaScript y React. Apasionado por crear interfaces de usuario intuitivas y estéticamente agradables, disfruta trabajando con frameworks modernos y creando manuales bien ...

Leer más