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?
If you look at the IL code, the static class will be abstract
and sealed
which gives two important qualities:
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;
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With