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

前端 未结 16 2782
盖世英雄少女心
盖世英雄少女心 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条回答
  • 2020-11-22 02:16

    mmm...

    Static means that you can access that function without having an instance of the class.

    You can access directly from the class definition.

    0 讨论(0)
  • 2020-11-22 02:17

    public - can be access by anyone anywhere.
    private - can only be accessed from with in the class it is a part of.
    protected - can only be accessed from with in the class or any object that inherits off of the class.

    Nothing is like null but in VB.
    Static means you have one instance of that object, method for every instance of that class.

    0 讨论(0)
  • 2020-11-22 02:18

    Reposting the awesome diagrams from this answer.

    Here are all access modifiers in Venn diagrams, from more limiting to more promiscuous:

    private:
    enter image description here

    private protected: - added in C# 7.2
    enter image description here

    internal:
    enter image description here

    protected:
    enter image description here

    protected internal:
    enter image description here

    public:
    enter image description here

    0 讨论(0)
  • 2020-11-22 02:18

    Regarding the question of Nothing

    • Namespace types are internal by default
    • Any type member, including nested types are private by default
    0 讨论(0)
提交回复
热议问题