Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Access Modifier

 Access Modifiers are keywords that define the accessibility of a member, class or datatype
in a program. These are mainly used to restrict unwanted data manipulation by external
programs or classes.
 To control the visibility of class members (the security level of each individual class and
class member)
 To achieve “Encapsulation” - which is the process of making sure that “sensitive” data is
hidden from users. This is done by declaring fields as private.

Note: - By default, all members of a class are private if you don't specify an access modifier

 There are 4 access modifiers. They are


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

Public modifier: -

 The code is accessible for all classes


 Access is granted to the entire program. This means that another method or another
assembly which contains the class reference can access these members or types.
 This access modifier has the most permissive access level in comparison to all other access
modifiers
 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");  
        }  
    }  

Private modifier: -
 The code is only accessible within the same class
 If you declare a field with a private access modifier, it can only be accessed within the same
class.
 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");  
        }  
    }  
}  

Protected modifier: -
 The code is accessible within the same class, or in a class that is inherited from that class.
 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");  
        }  
    }  
}  
Internal modifier: -
 The code is only accessible within its own assembly, but not from another assembly.
 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");  
        }  
    }  
}  

You might also like