Search code examples
c#oopstaticstatic-methodsnon-static

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?


Solution

  • 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; }
    }