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

前端 未结 4 1702
悲&欢浪女
悲&欢浪女 2020-12-02 20:28

I have two classes Class A and ClassB:

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

class Cla         


        
4条回答
  •  不思量自难忘°
    2020-12-02 20:44

    If you have a non-static class containing only static methods, you could create an instance of that class. But you can't use that instance meaningfully. NB: when you don't define a constructor, the compiler adds one for you.

    A static class does not have a constructor, so you can't create an instance of it. Also the compiler gives an error when you add an instance method to it (where you meant a static method).

    See also docs.

提交回复
热议问题