11

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?

PoLáKoSz
  • 355
  • 1
  • 6
  • 7
Athiwat Chunlakhan
  • 7,589
  • 14
  • 44
  • 72

7 Answers7

15

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;
    }
}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
6

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
Gishu
  • 134,492
  • 47
  • 225
  • 308
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.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
3

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.

German Latorre
  • 10,058
  • 14
  • 48
  • 59
3

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.

Charlie
  • 10,227
  • 10
  • 51
  • 92
1

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

public static class Storage
{
   public static string filePath { get; set; }
}

in this,the class need not to be instantiate.so same with the filepath ,it will occupy unique value of class Storage for all object.

public class Storage
{
    private void Storage {};
    public static string filePath { get; set; }
}  

in this,the class is non static,need to be instantiate

PoLáKoSz
  • 355
  • 1
  • 6
  • 7
JavaResp
  • 2,253
  • 5
  • 24
  • 25
0

As we know variables and functions are of two types-instance and class.

A static class -has only class variables no instance variables.

Hence cannot be instantiated,only accessible by Classname.method().

It contains only Private constructors no public constructor is there.

A static class contains only static members.