In C#, what is the difference between public, private, protected, and having no access modifier?

前端 未结 16 2760
盖世英雄少女心
盖世英雄少女心 2020-11-22 01:12

All my college years I have been using public, and would like to know the difference between public, private, and protected

16条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 01:59

    Public - If you can see the class, then you can see the method

    Private - If you are part of the class, then you can see the method, otherwise not.

    Protected - Same as Private, plus all descendants can also see the method.

    Static (class) - Remember the distinction between "Class" and "Object" ? Forget all that. They are the same with "static"... the class is the one-and-only instance of itself.

    Static (method) - Whenever you use this method, it will have a frame of reference independent of the actual instance of the class it is part of.

提交回复
热议问题