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

c# access modifiers
default access modifier in c#
c# private protected
access modifiers in c# with example program
default access modifier for interface in c#
c# protected
protected internal c#
internal access modifier in c#

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

Also what does static do as opposed to having nothing?


Access modifiers

From docs.microsoft.com:

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private

The type or member can only be accessed by code in the same class or struct.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

private protected (added in C# 7.2)

The type or member can only be accessed by code in the same class or struct, or in a derived class from the same assembly, but not from another assembly.

internal

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

When no access modifier is set, a default access modifier is used. So there is always some form of access modifier even if it's not set.

static modifier

The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:

static class Foo()
{
    static Foo()
    {
        Bar = "fubar";
    }

    public static string Bar { get; set; }
}

Static classes are often used as services, you can use them like so:

MyStaticClass.ServiceMethod(...);

Access Modifiers, Use the access modifiers, public , protected , internal , or private , to specify one of If no access modifier is specified in a member declaration, a default Nested types, which are members of other types, can have declared  publicThe type or member can be accessed by any other code in the same assembly or another assembly that references it.privateThe type or member can only be accessed by code in the same class or struct.protectedThe type or member can only be accessed by code in the same class or struct, or in a derived class.internalThe type or member can be accessed by any code in the same assembly, but not from another assembly.protected internalThe type or member can be accessed by any code in the same


A graphical overview (summary in a nutshell)

Since static classes are sealed, they cannot be inherited (except from Object), so the keyword protected is invalid on static classes.

For the defaults if you put no access modifier in front, see here: Default visibility for C# classes and members (fields, methods, etc.)?

Non-nested

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

Nested:

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

Also, there is they sealed-keyword, which makes a class not-inheritable. Also, in VB.NET, the keywords are sometimes different, so here a cheat-sheet:

Accessibility Levels, For a comparison of private protected with the other access modifiers, see public class BaseClass { private protected int myValue = 0; } public  Enumeration members are always public, and no access modifiers can be applied. Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested. C# language specification. For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.


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.

private protected, Access Modifiers are keywords that define the accessibility of a member, There are 4 access modifiers (public, protected, internal, private) which Derived types within current assembly, Yes, Yes, Yes, Yes, No, Yes This access modifier has the most permissive access level in comparison to all other access modifiers. Generally, in c# only one access modifier is allowed to use with any member or type, except when we use protected internal or private protected combinations. In c#, we are not allowed to use any access modifiers on namespaces , because the namespaces have no access restrictions.


Reposting the awesome diagrams from this answer.

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

private:

private protected: - added in C# 7.2

internal:

protected:

protected internal:

public:

Access Modifiers in C#, The following are the different types of access modifiers available in c# In c#, we are not allowed to use any access modifiers on namespaces, because the Following is the example of defining members with a public modifier in c#  The default access for members and classes is private. Example class Base { public: // public members go here protected: // protected members go here private: // private members go here }; A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member.


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();
        }
    }
}

C# Access Modifiers (Public, Private, Protected, Internal), https://code-maze.com/csharp-basics-access-modifiers/ Assembly1.cs; // Compile with: /target:library; public class BaseClass; {; private protected int myValue = 0;; } Private Protected Access in Different Assembly(not valid). private protected (C# Reference) 11/15/2017; 2 minutes to read +4; In this article. The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly.


Difference between protected and private protected, C# access modifiers including private, public, protected, and internal define the scope of a For example, a public class is accessible to everyone without any restrictions, In C# there are 6 different types of Access Modifiers. 2 Answers. public means this variable or method is available everywhere in the application, private means the variable or method is available in the class it was defined alone, while protected means the variable or method is available both in the class it was defined and every subclass (every class that inherits from the base class) Soner Gönül, Software Developer from Turkey.


What Are Access Modifiers In C#, Moreover, all the C# types have access modifiers implemented, even if they are not stated (default access modifier is applied then). C# provides four types of access modifiers: private, public, protected, internal, and two  In December 2017, Microsoft released C# version 7.2 and within that, it introduced a new compound access modifier - Private Protected. While the existing modifiers have become very common across the C# developer community, there is still confusion going on about private protected.


C# Basics - Access Modifiers in C#, There is four access modifier in Java namely public, private, protected and the use private or protected modifier with a top level class but you can use public Java automatically apply a default modifier (no it doesn't use default keyword),  C# provides four types of access modifiers: private, public, protected, internal, and two combinations: protected-internal and private-protected. Private Access Modifier. Objects that implement private access modifier are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created: