tags:

views:

566

answers:

7

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?

+1  A: 

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
+6  A: 

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
Eh? Your example is not a static class?
Thorarin
That isn't a static class though?
Svish
@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)
Fredrik Mörk
+1  A: 

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 me instantiated, inherited, and can have non-static members.

antur123
+1  A: 

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
+2  A: 

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
A: 

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

JavaResp
A: 

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.

Somya Chaurasia