C# Sealed Class Tutorial with Examples

C# Sealed Class Tutorial with Examples

Today, We want to share with you C# Sealed Class Tutorial with Examples.
In this post we will show you Sealed Class in C#, hear for c# – Static and Sealed class differences we will give you demo and example for implement.
In this post, we will learn about Sealed Classes in C# with an example and explanation with an example.

Sealed Class in C#

In this post,we will learn about Sealed Class in C# with an example.I will also review why programming expert use sealed classes in their code and products.

Now in this post, I will explain about Sealed Class in C# with appropriate example.

Sealed classes are used to limit(restrict) the inheritance feature of oop.If class is defined as a sealed class, the class cannot be inherited. 

READ :  C# Sorted Dictionary Tutorial with Examples


In C#, the sealed modifier or keyword is used to define a class as sealed. In Visual Basic .NET the NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class then the compiler throws an error and mark it as invalid.

Now create Console Application in Visual Studio and write below lines of code in it.

using System;
using System.Collections;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            SealedClass sealedCls = new SealedClass();

            int total = sealedCls.AddNumber(7, 3);

            Console.WriteLine("Sum = " + total.ToString());
            Console.ReadLine();
        }
    }
    sealed class SealedClass
    {
        public int AddNumber(int x, int y)
        {
            return x + y;
        }
    }  
}

Why Sealed Classes?

The basic intesnsion of a sealed class is to take away the inheritance feature from the user so they cannot derive a class from a sealed class. One of the best usage of sealed classes is when you have a class with static members.

READ :  Angular ng init directive Example

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Abstract and Sealed Classes and Class Members in C#.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Add a Comment

Your email address will not be published. Required fields are marked *