Search code examples
c#classmethodsstaticstatic-classes

What is the difference between putting a static method in a static class and putting an instance method in a static class?


What is the difference between these two classes?

public static class MyClass
{
    public static string SayHello()
    {
        return "Hello";
    }
}

public static class MyClass
{
    public string SayHello()
    {
        return "Hello";
    }
}

Is the second SayHello method also static since it is on a static class? If so, is there any reason to include the static keyword on methods when they are defined in a static class?


Solution

  • The second example is not even possible to do in c#, you will get compile time error:

    'SayHello': cannot declare instance members in a static class

    So you must declare members of static calss with static keyword.