Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
2.38/5 (4 votes)
Hi Dear friend ,

I have one question

i have study both class static or sealed class

1)Both class can't be inheritance
2)we can not create object
3) we directly use by name

but what is the differences in these two class

i think all this answer as fresher level .
but i want answer as a experience person


Thanks in advance
Anil kumar
Posted
Updated 28-Oct-17 18:03pm

They are completely different.

A sealed class cannot be inherited: that is it's only difference from any other type of class.

A static class cannot be instantiated. You cannot, at any time write new MyStaticClass() without the compiler complaining. It never has a this reference, and the fields, properties and methods can only ever be accessed via the class name: MyStaticClass.MyMethod() In addition, you cannot derive from a static class, not can a static class derive from any class other than object
 
Share this answer
 
Comments
RaisKazi 19-Sep-11 5:19am    
my 5!
A static class can only contain static Methods, Properties and Fields and what you wrote in 1), 2) and 3) applies.
A sealed class is a normal class which you can make an instance of, but you cannot inherit from it. what you wrote in 2) and 3) does not apply to sealed classes, tough a sealed class can have static methods and thus you can argue 3) applies.

For a more indepth view see Static Classes and Static Class Members[^], sealed (C# Reference)[^] and Abstract and Sealed Classes and Class Members[^]
 
Share this answer
 
v2
Comments
RaisKazi 19-Sep-11 5:18am    
my 5!
Simon Bang Terkildsen 19-Sep-11 7:43am    
Thank you, Rais
Sealed classes: 
1)Can create instances, but cannot inherit
2)Can contain static as well as nonstatic members.

Static classes:
1)Can neither create their instances, nor inherit them
2)Can have static members only.
 
Share this answer
 
Comments
RaisKazi 19-Sep-11 5:18am    
my 5!
Shambhoo kumar 28-Jan-13 6:57am    
great difference...thanks alot..
my 5!
:)
1. A Sealed class cannot be inherited from and can static and non-static members or methods. Contains a public constructor.
2. A Static class cannot be inherited from and contains only static methods and class properties. Contains a private constructor.
 
Share this answer
 
Comments
Preethi PS 22-Aug-12 3:08am    
Why cant a static class be inherited?
* A static class can only contain static members (it is just a container for methods that do not logically belong to an instance of any standard class)
* An abstract class can cotnain all usual kinds of members (static, abstract and also instance)
 
Share this answer
 
but some points is clear this topic:-
1) Both static and seales can't be inherited but we can inherit static class by other
static class, where as sealed can't be never ihherited.
2). we can't craete object of static class where as we can create object of sealed class.
3). we can not use sealed class as a base class where as static can be use as a base class.
 
Share this answer
 
Comments
CHill60 29-Oct-17 3:54am    
Those points were made clear 6 years ago. Stick to answering new questions where the OP still needs help
phil.o 29-Oct-17 10:16am    
"we can inherit static class by other static class"
"static can be use as a base class"
These statements are false.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900