TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Access Modifiers

C# Access Modifiers for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Access Modifiers / Specifiers

C# Access modifiers or specifiers are the keywords that are used to specify accessibility or scope of variables and functions in the C# application.

C# provides five types of access specifiers.

  1. Public
  2. Protected
  3. Internal
  4. Protected internal
  5. Private

We can choose any of these to protect our data. Public is not restricted and Private is most restricted. The following table describes about the accessibility of each.

Access Specifier Description
Public It specifies that access is not restricted.
Protected It specifies that access is limited to the containing class or in derived class.
Internal It specifies that access is limited to the current assembly.
protected internal It specifies that access is limited to the current assembly or types derived from the containing class.
Private It specifies that access is limited to the containing type.

Now, let's create examples to check accessibility of each access specifier.


1) C# Public Access Specifier

It makes data accessible publicly. It does not restrict data to the declared block.

Example

using System;
namespace AccessSpecifiers
{
    class PublicTest
    {
        public string name = "Shantosh Kumar";
        public void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            PublicTest publicTest = new PublicTest();
            // Accessing public variable
            Console.WriteLine("Hello " + publicTest.name);
            // Accessing public function
            publicTest.Msg("Peter Decosta");
        }
    }
}

Output:

Hello Shantosh Kumar
Hello Peter Decosta

2) C# Protected Access Specifier

It is accessible within the class and has limited scope. It is also accessible within sub class or child class, in case of inheritance.

Example

using System;
namespace AccessSpecifiers
{
    class ProtectedTest
    {
        protected string name = "Shashikant";
        protected void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ProtectedTest protectedTest = new ProtectedTest();
            // Accessing protected variable
            Console.WriteLine("Hello "+ protectedTest.name);
            // Accessing protected function
            protectedTest.Msg("Swami Ayyer");
        }
    }
}

Output:

Compile time error
'ProtectedTest.name' is inaccessible due to its protection level.

Example2

Here, we are accessing protected members within child class by inheritance.

using System;
namespace AccessSpecifiers
{
    class ProtectedTest
    {
        protected string name = "Shashikant";
        protected void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program : ProtectedTest
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            // Accessing protected variable
            Console.WriteLine("Hello " + program.name);
            // Accessing protected function
            program.Msg("Swami Ayyer");
        }
    }    
}

Output:

Hello Shashikant
Hello Swami Ayyer

3) C# Internal Access Specifier

The internal keyword is used to specify the internal access specifier for the variables and functions. This specifier is accessible only within files in the same assembly.

Example

using System;
namespace AccessSpecifiers
{
    class InternalTest
    {
        internal string name = "Shantosh Kumar";
        internal void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            InternalTest internalTest = new InternalTest();
            // Accessing internal variable
            Console.WriteLine("Hello " + internalTest.name);
            // Accessing internal function
            internalTest.Msg("Peter Decosta");
        }
    }
}

Output:

Hello Shantosh Kumar
Hello Peter Decosta

4) C# Protected Internal Access Specifier

Variable or function declared protected internal can be accessed in the assembly in which it is declared. It can also be accessed within a derived class in another assembly.

Example

using System;
namespace AccessSpecifiers
{
    class InternalTest
    {
        protected internal string name = "Shantosh Kumar";
        protected internal void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            InternalTest internalTest = new InternalTest();
            // Accessing protected internal variable
            Console.WriteLine("Hello " + internalTest.name);
            // Accessing protected internal function
            internalTest.Msg("Peter Decosta");
        }
    }
}

Output:

Hello Shantosh Kumar
Hello Peter Decosta

5) C# Private Access Specifier

Private Access Specifier is used to specify private accessibility to the variable or function. It is most restrictive and accessible only within the body of class in which it is declared.

Example

using System;
namespace AccessSpecifiers
{
    class PrivateTest
    {
        private string name = "Shantosh Kumar";
        private void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            PrivateTest privateTest = new PrivateTest();
            // Accessing private variable
            Console.WriteLine("Hello " + privateTest.name);
            // Accessing private function
            privateTest.Msg("Peter Decosta");
        }
    }
}

Output:

Compile time error
'PrivateTest.name' is inaccessible due to its protection level.

C# Private Specifier Example 2

using System;
namespace AccessSpecifiers
{
    class Program
    {
        private string name = "Shantosh Kumar";
        private void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
        static void Main(string[] args)
        {
            Program program = new Program();
            // Accessing private variable
            Console.WriteLine("Hello " + program.name);
            // Accessing private function
            program.Msg("Peter Decosta");
        }
    }
}

Output:

Hello Shantosh Kumar
Hello Peter Decosta

Next TopicC# Encapsulation




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf