1

I know this is meaning less question, but I'm confused why sealed class create its instance.

public sealed class SealedClass1
{
    static SealedClass1()
    {
    }

    public string GetmeassageFromSealed(string mesage)
    {
        return mesage;
    }
}

Here i decorate my constructor as private

public sealed class SealedClass1
        {
            static SealedClass1()
            {

           }
      private Singleton()
            {

            }
            public string GetmeassageFromSealed(string mesage)
            {
                return mesage;
            }
        }
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Md Ghousemohi
  • 197
  • 2
  • 13

2 Answers2

2

The sealed keyword in C# means that no class can inherit from that class.

So if you have this scenario:

public class Test { }

The following is valid

public class NewTest: Test { }

However, as soon as you add the sealed keyword, the compiler with throw an error when trying to compile that library.

public sealed class Test { }

public class NewTest: Test { }

This would throw

'NewTest' cannot inherit from sealed class 'Test'.

In short, the sealed keyword is a compile time check to ensure that there is no inheritence.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
1

A Sealed Class can be instantiated, also it can inherit from other classes but it can not be inherited by other classes.