Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This doubt is killing me for many months...
I want to know the difference & usages of Static Class and Static Methode...
FOr Eg: I have a class like this

C#
static class ABC
{
    public int a ; 
    public void function_a()
    {
      a = 10;
    }
}


and another class like this

C#
class DEF
{
    public static int a ;
    public static void function_a()
    {
       a= 10;
    }
}


I have used the second type of class many times,,, and I know the usage....
But what's the usage of First type of CLass ..... Please Answer :(
Posted
Comments
Nirav Prabtani 15-Jul-13 10:33am    
have you tried on google....?????
Yesudasan Moses 15-Jul-13 10:41am    
yea,,, but why do we need it ??
I dont see any difference when I put "static keyword" before my class having only static members....

Your first example will not compile, a static class must have all static members.

The difference between just using some static methods and a static class is that you are telling the compiler that the class cannot be instantiated. The second example you can create an object of the DEF class even though there are no instance methods in it. The ABC class cannot be instantiated with the new operator (will get a compile-time error).
 
Share this answer
 
v2
A reference from MSDN

When to Use Static Classes
--------------------------------------------------------------------------------

Suppose you have a class CompanyInfo that contains the following methods to get information about the company name and address.

C#
 class CompanyInfo
{
    public string GetCompanyName() { return "CompanyName"; }
    public string GetCompanyAddress() { return "CompanyAddress"; }
    //...
}

These methods do not need to be attached to a specific instance of the class. Therefore, instead of creating unnecessary instances of this class, you can declare it as a static class, like this:

C#
C#
 static class CompanyInfo
{
    public static string GetCompanyName() { return "CompanyName"; }
    public static string GetCompanyAddress() { return "CompanyAddress"; }
    //...
}

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.
 
Share this answer
 

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