What is a sealed class in C#?

CsharpProgrammingServer Side Programming

Sealed class in C# with the sealed keyword cannot be inherited. In the same way, the sealed keyword can be added to the method.

When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.

Let us see an example of sealed class in C# −

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Result ob = new Result();
         string str = ob.Display();

         Console.WriteLine(str);
         Console.ReadLine();
      }
   }

   public sealed class Result {
      public string Display() {
         return "Passed";
      }
   }
}

To access the members of the sealed class, we need to create the object. The method created inside the sealed class cannot be inherited −

public sealed class Result {
   public string Display() {
      return "Passed";
   }
}
raja
Published on 13-Aug-2018 15:50:41
Advertisements