The sealed keyword in C# is used to prevent a class or method from being inherited or overridden further. It can be applied to:
1. Sealed Classes
A sealed class is useful when you want to restrict the class from being a base class, ensuring no other class can derive from it.
public sealed class ClassName
{
// Members of the class
}
Real-Time Example: Logger Utility
A logging utility class is often made sealed to ensure it cannot be extended, preserving its behavior.
public sealed class Logger
{
private static Logger _instance;
// Private constructor to prevent instantiation
private Logger() { }
public static Logger Instance => _instance ??= new Logger();
public void Log(string message)
{
Console.WriteLine($"Log: {message}");
}
}
Usage
var logger = Logger.Instance;
logger.Log("Application started.");
// Inheriting Logger is not allowed due to the sealed keyword
2. Sealed Methods
A sealed method is used in a derived class to prevent further overriding. This is useful when you want to finalize a method’s behavior in a derived class.
public class BaseClass
{
public virtual void Display()
{
Console.WriteLine("Base Class Display");
}
}
public class DerivedClass : BaseClass
{
public sealed override void Display()
{
Console.WriteLine("Derived Class Display");
}
}
public class SubDerivedClass : DerivedClass
{
// Cannot override Display method as it is sealed in DerivedClass
}
Real-Time Example: Payment Processing System
Imagine a payment system where a ProcessPayment method in a derived class should not be overridden further for security reasons.
public class PaymentProcessor
{
public virtual void ProcessPayment(decimal amount)
{
Console.WriteLine($"Processing payment of {amount:C}");
}
}
public class SecurePaymentProcessor : PaymentProcessor
{
public sealed override void ProcessPayment(decimal amount)
{
// Add security checks
Console.WriteLine($"Securely processing payment of {amount:C}");
}
}
// Any further derived class cannot override ProcessPayment
Usage
PaymentProcessor processor = new SecurePaymentProcessor();
processor.ProcessPayment(100.00m);
// Further overrides of ProcessPayment are not allowed
When to Use the sealed Keyword?
Key Points:
The sealed keyword in C# is used to prevent a class or method from being inherited or overridden further. It can be applied to:
1. Sealed Classes
A sealed class is useful when you want to restrict the class from being a base class, ensuring no other class can derive from it.
public sealed class ClassName { // Members of the class }
Real-Time Example: Logger Utility
A logging utility class is often made sealed to ensure it cannot be extended, preserving its behavior.
public sealed class Logger { private static Logger _instance; // Private constructor to prevent instantiation private Logger() { } public static Logger Instance => _instance ??= new Logger(); public void Log(string message) { Console.WriteLine($"Log: {message}"); } }
Usage
var logger = Logger.Instance; logger.Log("Application started."); // Inheriting Logger is not allowed due to the sealed keyword
2. Sealed Methods
A sealed method is used in a derived class to prevent further overriding. This is useful when you want to finalize a method’s behavior in a derived class.
public class BaseClass { public virtual void Display() { Console.WriteLine("Base Class Display"); } } public class DerivedClass : BaseClass { public sealed override void Display() { Console.WriteLine("Derived Class Display"); } } public class SubDerivedClass : DerivedClass { // Cannot override Display method as it is sealed in DerivedClass }
Real-Time Example: Payment Processing System
Imagine a payment system where a ProcessPayment method in a derived class should not be overridden further for security reasons.
public class PaymentProcessor { public virtual void ProcessPayment(decimal amount) { Console.WriteLine($"Processing payment of {amount:C}"); } } public class SecurePaymentProcessor : PaymentProcessor { public sealed override void ProcessPayment(decimal amount) { // Add security checks Console.WriteLine($"Securely processing payment of {amount:C}"); } } // Any further derived class cannot override ProcessPayment
Usage
PaymentProcessor processor = new SecurePaymentProcessor(); processor.ProcessPayment(100.00m); // Further overrides of ProcessPayment are not allowed
When to Use the sealed Keyword?
Key Points:
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved