0

Can you please tell me what is the difference between following two class

public static class Product
{
    public static int AddData(int x, int y)
    {
        return x + y;
    }
}

public class Product
{
    public static int AddData(int x, int y)
    {
        return x + y;
    }
} 

since we can able to access AddData method in same way in both class.

  • You cannot instantiate the first class. So if you want to add another method to this class it has to be static. Second class can be instantiated so you can also add non static methods to it (this is just one point, which came to my mind). – user3596113 Jun 11 '15 at 10:22
  • Also see http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp. – Mihai8 Jun 11 '15 at 10:23

1 Answers1

0
  1. In static classes you can only add static methods.

  2. You cannot create instance of that static class i.e. it cannot be assigned to a variable

  3. Also the access to the static method in static class is faster than instance one.

  4. Static classes also cannot be inherited (it is meaningless since they cannot have instances)

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123