how to call static method inside non static method in c#

前端 未结 4 914
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 05:58

how to call static method inside non static method in c# ?

Interviewer gave me scenario :

class class1
{

    pub         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-24 06:59

    A normal practice is to call static method with class name.

    See: Static Classes and Static Class Members (C# Programming Guide)

    The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

    So your call would be like:

    class1.method1();
    

    But it is not necessary

    You can call the static method without class name like:

    method1();
    

    But you can only do that inside the class which holds that static method, you can't call static method without class name outside of that class.

提交回复
热议问题