Sealed Class Example in C#

Learn, how to implement sealed class in C#?
Submitted by Nidhi, on August 18, 2020 [Last updated : March 21, 2023]

Here we will demonstrate the example of a sealed class. As we know that if we create a class as a sealed class then it is restricted that we cannot inherit that class into another class.

C# program to demonstrate the example of a sealed class

The source code to demonstrate the example of a sealed class in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

// Program to demonstrate the 
// example of a sealed class in C#.

using System;

sealed class MySealedClass
{
    public int num1;
    public int num2;

    public MySealedClass()
    {
        num1 = 500;
        num2 = 800;
    }
}

class SealedTest
{
    static void Main()
    {
        MySealedClass OB = new MySealedClass();
        
        Console.WriteLine(OB.num1);
        Console.WriteLine(OB.num2);
    }
}

Output

500
800
Press any key to continue . . .

Explanation

In the above program, we created a sealed class MySealedClass that contains two public data members initialized with 500 and 800 in the constructor of the same class.

We created one more class SealedTest that contains Main() method, in the Main() method we created object OB of MySealedClass and print the value of data members on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.