c# What is the different between static class and non-static (I am talking about the class itself not the field)
Solution 1
If you look at the IL code, the static class will be abstract
and sealed
which gives two important qualities:
- You cannot create instances from it
- It cannot be inherited
A consequence of the first point is that a static class cannot contain non-static members. There may be many uses of static members in a non-static class. One common use is to have a class factory:
public class SomeClass
{
public int SomeInt { get; set; }
public static SomeClass Create(int defaultValue)
{
SomeClass result = new SomeClass();
result.SomeInt = defaultValue;
return result;
}
}
Solution 2
Here is the official/MSDN hot-spot to learn about static classes
The main features of a static class are:
* They only contain static members.
* They cannot be instantiated.
* They are sealed.
* They cannot contain Instance Constructors
Basically a static class is identical to a 'normal'/non-static class which has only static methods and a private ctor. Marking it as static helps clarify intent and helps the compiler do some compile-time checks to disallow certain things e.g. disallow instantiation.
Real-world uses I can think of: Use it to house or as a way to organize
- utility methods (methods not associated with any instance of a type) e.g. Math for Min and Max methods
- extension methods e.g. StopWatchExtensions for a Reset method on a StopWatch
Solution 3
Lots of classes have both instance and static methods. String for example has:
String.Format(string, arg0, arg1, arg2) // static method
And
String myString = " Hello world!";
myString = myString.Substring(4); // instance method
If you're asking why both the class and the method need the static keyword it's simply by design. I see what you're asking, if the class is static then of course all the methods are static as well, seems kind of redundant to put it there twice. I don't know if there's a good reason for that or not.
Solution 4
Static classes are only available from C#2 upwards. In C#1 you would have to seal your class and specify that it is not instantiable by added a private constructor to get this kind of behaviour.
Solution 5
When you declare a class as static:
- It is allowed to have only static members,
- It cannot be instantiated (it has no public constructor), and
- It cannot be inherited (it's sealed).
Any class which is not declared as static can be instantiated, inherited, and can have non-static members.
Comments
-
Athiwat Chunlakhan almost 4 years
The syntax maybe wrong
public static class Storage { public static string filePath { get; set; } }
And
public class Storage { private void Storage () {}; public static string filePath { get; set; } }
I got this from an example on the internet. what is the use of the second one?
-
Lemon almost 15 yearsThat isn't a static class though?
-
Fredrik Mörk almost 15 years@Thorarin and @Svish: that is correct. It is serving to answer the last question of the use of static members in non-static types (as I interpreted the past question in the original post)