What's the difference between public, private, protected, and no access modifiers in C?

I have been using public all my college years. Do you want to know the difference between public, private and protected?

What does static do instead of doing nothing?

#1st floor

Graphic overview (Overview)

For the default settings (if you did not place any access modifiers before), see here:
Default visibility of C classes and members (fields, methods, etc.)?

Non nesting

enum                              public
non-nested classes / structs      internal
interfaces                        internal
delegates in namespace            internal
class/struct member(s)            private
delegates nested in class/struct  private

Nesting:

nested enum      public
nested interface public
nested class     private
nested struct    private

#2nd floor

using System;

namespace ClassLibrary1
{
    public class SameAssemblyBaseClass
    {
        public string publicVariable = "public";
        protected string protectedVariable = "protected";
        protected internal string protected_InternalVariable = "protected internal";
        internal string internalVariable = "internal";
        private string privateVariable = "private";
        public void test()
        {
            // OK
            Console.WriteLine(privateVariable);

            // OK
            Console.WriteLine(publicVariable);

            // OK
            Console.WriteLine(protectedVariable);

            // OK
            Console.WriteLine(internalVariable);

            // OK
            Console.WriteLine(protected_InternalVariable);
        }
    }

    public class SameAssemblyDerivedClass : SameAssemblyBaseClass
    {
        public void test()
        {
            SameAssemblyDerivedClass p = new SameAssemblyDerivedClass();

            // NOT OK
            // Console.WriteLine(privateVariable);

            // OK
            Console.WriteLine(p.publicVariable);

            // OK
            Console.WriteLine(p.protectedVariable);

            // OK
            Console.WriteLine(p.internalVariable);

            // OK
            Console.WriteLine(p.protected_InternalVariable);
        }
    }

    public class SameAssemblyDifferentClass
    {
        public SameAssemblyDifferentClass()
        {
            SameAssemblyBaseClass p = new SameAssemblyBaseClass();

            // OK
            Console.WriteLine(p.publicVariable);

            // OK
            Console.WriteLine(p.internalVariable);

            // NOT OK
            // Console.WriteLine(privateVariable);

            // Error : 'ClassLibrary1.SameAssemblyBaseClass.protectedVariable' is inaccessible due to its protection level
            //Console.WriteLine(p.protectedVariable);

            // OK
            Console.WriteLine(p.protected_InternalVariable);
        }
    }
}
 using System;
        using ClassLibrary1;
        namespace ConsoleApplication4

{
    class DifferentAssemblyClass
    {
        public DifferentAssemblyClass()
        {
            SameAssemblyBaseClass p = new SameAssemblyBaseClass();

            // NOT OK
            // Console.WriteLine(p.privateVariable);

            // NOT OK
            // Console.WriteLine(p.internalVariable);

            // OK
            Console.WriteLine(p.publicVariable);

            // Error : 'ClassLibrary1.SameAssemblyBaseClass.protectedVariable' is inaccessible due to its protection level
            // Console.WriteLine(p.protectedVariable);

            // Error : 'ClassLibrary1.SameAssemblyBaseClass.protected_InternalVariable' is inaccessible due to its protection level
            // Console.WriteLine(p.protected_InternalVariable);
        }
    }

    class DifferentAssemblyDerivedClass : SameAssemblyBaseClass
    {
        static void Main(string[] args)
        {
            DifferentAssemblyDerivedClass p = new DifferentAssemblyDerivedClass();

            // NOT OK
            // Console.WriteLine(p.privateVariable);

            // NOT OK
            //Console.WriteLine(p.internalVariable);

            // OK
            Console.WriteLine(p.publicVariable);

            // OK
            Console.WriteLine(p.protectedVariable);

            // OK
            Console.WriteLine(p.protected_InternalVariable);

            SameAssemblyDerivedClass dd = new SameAssemblyDerivedClass();
            dd.test();
        }
    }
}

#3rd floor

Private state indicates that variables can only be accessed by objects of the same class. The protected state extends access to descendants of this class.

"From the above table we can see the difference between private and protected I think the two are the same So what do these two separate commands need? "

inspect MSDN Link for more information

#4th floor

There are 6 access modifiers for C:

private: member declarations with this accessibility can be seen within the containing type, and for any derived type, other types in the same assembly or types outside the containing assembly are not visible. That is, access is limited to include types.

protected: the member declaration with this accessibility is visible in both the containing type derived types that contain the assembly and the containing type derived types outside the containing assembly. That is, access is limited to derived types that contain types.

internal: a member declaration with this accessibility can be seen in the assembly containing the member, but not in any program other than the assembly. That is, access is limited to include assemblies only.

Internally protected: declare that members with this accessibility are visible within a type derived from a containing type, either internal or external to the containing assembly, and for any type within the containing assembly. That is, access is limited to include assemblies or derived types.

public: a member declaration with this accessibility can be visible in the assembly that contains the member, or in any other assembly that references the containing assembly. That is, access is unrestricted.

C 7.2 added a new accessibility level:

private protected: member declarations with this accessibility can be seen in types that contain assemblies derived from this containing type. It is not visible to any type that is not derived from the containing type, or outside the containing assembly. That is, access is limited to derived types within the containing assembly.

Source code, including sample code for the new private protected access modifier

#5th floor

from This answer has been redistributed Great chart.

This is all the access modifiers in the Venn diagram, from more restricted to more hybrid:

private :

private protected : - Add in C 7.2

internal :

protected :

protected internal :

public :

Posted by abhishek on Mon, 23 Dec 2019 06:13:35 -0800