Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between static methods in a Non static class and static methods in a static class?

I have two classes Class A and ClassB:

static class ClassA {     static string SomeMethod()     {         return "I am a Static Method";     } }  class ClassB {     static string SomeMethod()     {         return "I am a Static Method";     } } 

I want to know what is the difference between ClassA.SomeMethod(); and ClassB.SomeMethod();

When they both can be accessed without creating an instance of the class, why do we need to create a static class instead of just using a non static class and declaring the methods as static?

like image 917
Vamsi Avatar asked Mar 09 '11 05:03

Vamsi


People also ask

What is the difference between a static method and a non static method?

Static method uses complie time binding or early binding. Non-static method uses run time binding or dynamic binding. A static method cannot be overridden being compile time binding. A non-static method can be overridden being dynamic binding.

What are the difference between static and non static members of class?

Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.

What is difference between static method and non static method in C#?

Difference between static and non-static classStatic class is defined using static keyword. Non-Static class is not defined by using static keyword. In static class, you are not allowed to create objects. In non-static class, you are allowed to create objects using new keyword.


1 Answers

The only difference is that static methods in a nonstatic class cannot be extension methods.


In other words, this is invalid:

class Test {     static void getCount(this ICollection<int> collection)     { return collection.Count; } } 

whereas this is valid:

static class Test {     static void getCount(this ICollection<int> collection)     { return collection.Count; } } 
like image 176
user541686 Avatar answered Oct 15 '22 12:10

user541686