152
votes
  1. Is there any class that be implemented in static class? means:

    static class ABC : Anyclass
    
  2. Is there any class which can be inherited in both sealed class and static class?
    means:

    static class ABC : AClass {}
    

    And

    sealed class ABC : AClass {}
    

May I be wrong in some extent?

6
static has really nothing to do with sealed - ken2k
But @ken2k, static classes are sealed by default in C#. Aren't they? static classes don't participate in inheritance at all. - RBT

6 Answers

672
votes

This may help you:

+--------------+---+-------------------------+------------------+---------------------+
|  Class Type  |   | Can inherit from others | Can be inherited | Can be instantiated | 
|--------------|---|-------------------------+------------------+---------------------+
| normal       | : |          YES            |        YES       |         YES         |
| abstract     | : |          YES            |        YES       |         NO          |
| sealed       | : |          YES            |        NO        |         YES         |
| static       | : |          NO             |        NO        |         NO          |
+--------------+---+-------------------------+------------------+---------------------+
40
votes

In simple words

Static Class

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Sealed Class

A sealed class cannot be used as a base class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.

19
votes

You can let a sealed class inherit from another class, but you cannot inherit from a sealed class:

sealed class MySealedClass : BaseClass // is ok
class MyOtherClass : MySealedClass     // won't compile

A static class cannot inherit from other classes.

5
votes

You can simply differentiate both of them as:

       Sealed Class       |        Static Class
--------------------------|-------------------------
it can inherit From other | it cannot inherit From other
classes but cannot be     | classes as well as cannot be
inherited                 | inherited
4
votes

Simple answer is a sealed class cannot be used as a base class.

I'm trying to show you sealed class is a derived class in the below code

 public sealed class SealedClass : ClassBase
{
    public override void Print()
    {
        base.Print();
    }
}

and another sealed feature is only accessible with instance from it.(you can not inherit from it)

 class Program
{
    static void Main(string[] args)
    {
        SealedClass objSeald = new SealedClass();
        objSeald.Name = "Blah blah balh";
        objSeald.Print();

    }
}
0
votes

1 - No, you can't implement a static class.

2 - No, you can't inherited from a static or sealed class