In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET, NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class, compiler throws an error.
Ex:
// Sealed class
sealed class clsSealedClass
{
}
· But if you try to use this class as a base class you will get error.
// Error will generate
sealed class clsMyClass: clsSealedClass
{
}
· This example demonstrate how to use the sealed class
using System;
class clsMyClass
{
static void Main()
{
clsSealedClass oclsSealedClass = new clsSealedClass ();
int total = oclsSealedClass.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
}
}
// Sealed class
sealed class clsSealedClass
{
public int Add(int x, int y)
{
return x + y;
}
}
Related Posts
Abstract Class In C#
Sealed Class In C#
Interface In C#
Abstract Class vs. Interface in C#
NET framework Comparison
C# Interview Questions