Saturday, 14 March 2020

Sealed Class Usage with Examples

Sealed will prevent the class from inheriting to other class.
Sealed class can contain both the static method and non static method.
Static method should be called without object creation.
Non static method should be called by creating the static method.

Example:

sealed class AA
    {
        public static string SealedwithStaticMethod()
        {
            return "SealedwithStaticMethod";
        }
        public  string SealedwithOUTStaticMethod()
        {
            return "SealedwithOUTStaticMethod";
        }
    }

Program.cs

Console.WriteLine(AA.SealedwithStaticMethod());
 AA dc = new AA();
Console.WriteLine(dc.SealedwithOUTStaticMethod());

OUTPUT: