Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • Difference between static and sealed classes in C#
AuthorQuestion
Difference between static and sealed classes in C#
Posted on: 28 Jun 2011
Static class:
1) A static class can not be instantiated. Means you can't create instance of          any static class.
2) A static class can have only static member (e.g static method, property, and         variables).
3) A static class restrict the user to call default constructor of the class.
4)
Static classes can only have static constructor to initialize static members.
5) Static classes are sealed so they can not be inherited.

Sealed Class:
1) When a class defined as sealed its not possible to inherit.
2)  A Sealed class is last class of Inheritance feature.
3) 
Use the sealed modifier in a class declaration to prevent inheritance of the         class.
4) It is not permitted to use the abstract modifier with a sealed class.
5) Structs are implicitly sealed; therefore, they cannot be inherited.
Example:
// cs_sealed_keyword.cs // Sealed classes using System; sealed class MyClass  {    public int x;     public int y; }  class MainClass  {    public static void Main()     {       MyClass mC = new MyClass();        mC.x = 110;       mC.y = 150;       Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);     } }
Output:
x = 110, y = 150
In the preceding example, if you attempt to inherit from the sealed class by using a statement like this:
class MyDerivedC: MyClass {} // Error
you will get the error message:
'MyDerivedC' cannot inherit from sealed class 'MyBaseC'.


Consultant
Solution Architect
AuthorReply
Re: Difference between static and sealed classes in C#
Posted on: 28 Jun 2011  
Note that your #5 for static classes says "Static classes are sealed so they can not be inherited". So all static classes are sealed but not all sealed classes are static. So the question "Difference between static and sealed classes in C#" is not relevant; they cannot be compared.

I know that it is common for the MSDN and for developers to say that a "static class can not be instantiated" but I think that is misleading. The truth is that static classes are always instantiated; there is always one and only one instance of a static class.

Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Difference between static and sealed classes in C#
Posted on: 28 Jun 2011  
As @Sam mentioned, there is only one instance of a static class. And also static class instance is created by CLR when the program or namespace containing static class is loaded into memory.
Re: Difference between static and sealed classes in C#
Posted on: 29 Jun 2011  
HI

Thanks for worth points


With Best Regards
Natarajan R (UI Designer)
KAPSYSTEM (www.kapsystem.com)

Re: Difference between static and sealed classes in C#
Posted on: 29 Jun 2011  
There isn't in fact a single instance of a static class which is represented in MSIL as an 'abstract sealed' class and so is neither instantiable nor inheritable. For example, this is how the System.Math static class is defined in MSIL:

.class public abstract auto ansi sealed beforefieldinit Math
    extends System.Object

As you'll see, technically, it inherits from System.Object but, since no instance is possible, the instance members which it inherits are not accessible.

Re: Difference between static and sealed classes in C#
Posted on: 29 Jun 2011  
The C# Language standard (ECMA-334) does say that static classes are classes "that are not intended to be instantiated", which coule be interpreted to mean that a static class is never created. We of course know that static classes are always created; they just cannot be created in code that we write. The standard does not define "instantiated".

So I will use a different term. I will say that static classes are always created by the compiler. If someone wants to use some other terminology then please tell us what terminology is better but the concept is an important concept.

Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Difference between static and sealed classes in C#
Posted on: 29 Jun 2011  
There's a difference between an instance of a type and a type's 'type object' which I suspect is what you're getting at here.

When a type (including a static class) is first accessed in an AppDomain, the CLR creates an instance of System.RuntimeType which is an internal class which inherits from the abstract System.Type class. This instance contains information about the type and there's only ever one of them for a given type. When you call typeof(SomeType) or various GetType() methods, it's a reference to this 'type object' which gets returned. 

An instance of a reference type, on the other hand, is (in effect) a block of memory on the heap which contains a reference to its type object, a sync block index and space to store its instance fields.

The point I'm making is that there are never any instances of the static class itself in constrast to a singleton which has exactly one instance. However, there are type objects for both static classes and singleton classes.

Re: Difference between static and sealed classes in C#
Posted on: 29 Jun 2011  
Listen. You don't have to call it an instance, but there is one something of a static class. So instead of saying that it is not an instance, what do you want to call it? Call it anything you want to calll it except "not an instance". As far as I am concerned, it is an instance created by the compiler but if that is not acceptable to you then tell us what to call it in terms that a beginner can understand. I assume that saying that "the CLR creates an instance of System.RuntimeType which is an internal class which inherits from the abstract System.Type class" does not help beginners to understand.

Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Difference between static and sealed classes in C#
Posted on: 29 Jun 2011  
I've already said that static classes have type objects but they don't have instances.

I'm not using the word 'instance' here in any esoteric sense but as another word for an object of a class which, hopefully, most OO programmers can understand.

As they're not publicly documented to my knowledge, I've no idea what the internal structures used by the JIT compiler are called but it's hardly a matter for beginners to concern themselves with.


Re: Difference between static and sealed classes in C#
Posted on: 29 Jun 2011  
OK, for those who are interested, I've just had a look at a book I have on the CLR by Jeffrey Richter and what happens is that the JIT compiler creates an internal data structure for each type used in a .NET application. This structure contains an address slot for each method in the type and will eventually (as methods are compiled to native code prior to being called for the first time) contain the memory address of the native code for that method.

Remembering that the only difference between static and instance methods is that the latter take a hidden 'this' pointer as their first argument, there is just one structure for each type. The name of the structure isn't given but it's effectively a memory address table.


SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.