News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Jan 3 2015 10:38PM     Mohammad Shaik
  0 Comments    1503 Views  
Here Mohammad Shaik provided a short tutorial with example, about Access Specifiers in C#.
What is Access Specifiers ?
  1. Access Modifiers (Access Specifiers) describes as the scope of accessibility of an Object and its members.
  2. All C# types and type members have an accessibility level.
  3. We can control the scope of the member object of a class using access specifiers.
  4. We are using access modifiers for providing security of our applications.
  5. When we specify the accessibility of a type or member we have to declare it by using any of the access modifiers provided by CSharp language.
Types of Access Specifiers:
  • public
  • private
  • protected
  • internal
  • protected internal
In tabular Form:
Modifier Description
public There are no restrictions on accessing public members.
private Access is limited to within the class definition. This is the default access modifier type if none is formally specified
protected Access is limited to within the class definition and any class that inherits from the class
internal Access is limited exclusively to classes defined within the current project assembly
protected internal Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.
public
  1. public is the most common access specifier in C#.
  2. It can be access from anywhere, that means there is no restriction on accessibility.
  3. The scope of the accessibility is inside class as well as outside.
  4. The type or member can be accessed by any other code in the same assembly or another assembly that references it.
There are no restrictions on accessing public members.
Accessibility:
  • Can be accessed by objects of the class
  • Can be accessed by derived classes
Example:

namespace MyApp1
{
    class access
    {
        // String Variable declared as public
        public string name;
        // Print method declared as public
        public void print()
        {
            Console.WriteLine("\nHi, My name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access objaccess = new access();
            Console.Write("Enter your name:\t");
            // Accepting value in public variable that is outside the class
            objaccess.name = Console.ReadLine();
            objaccess.print();

            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

Enter your name: Rejin
Hi, My name is Rejin

private
  1. The scope of the accessibility is limited only inside the classes or struct in which they are declared.
  2. The private members cannot be accessed outside the class and it is the least permissive access level.
Accessibility:
  • Cannot be accessed by object
  • Cannot be accessed by derived classes
Example:

namespace MyApp1
{
    class access
    {
        // String Variable declared as private
        private string name;
        // Method declared as Public
        public void print() 
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access objaccess = new access();
            Console.Write("Enter your name:\t");
            // raise error because of its protection level
            objaccess.name = Console.ReadLine();
            objaccess.print();
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

Output:
Error 1: Private_Access_Specifiers.access.name' is inaccessible due to its protection level

protected
  1. The scope of accessibility is limited within the class or struct and the class derived (Inherited )from this class.
  2. The protected access specifier hides its member variables and functions from other classes and objects.
  3. This type of variable or function can only be accessed in child class.
  4. It becomes very important while implementing inheritance.
Accessibility:
  • Cannot be accessed by object
  • By derived classes
Example:

namespace MyApp1
{
    class access
    {
        // String Variable declared as protected
        protected string name;
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access objaccess = new access();
            Console.Write("Enter your name:\t");
            // raise error because of its protection level
            objaccess.name = Console.ReadLine();
            objaccess.print();
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

Output:
'Protected_Specifier.access.name' is inaccessible due to its protection level.

This is because; the protected member can only be accessed within its child class. You can use protected access specifiers as follow:
Example:

using System;
 
namespace Protected_Specifier
{
  class access
   {
     // String Variable declared as protected
     protected string name;
     public void print()
      {
        Console.WriteLine("\nMy name is " + name);
      }
   }
 
  class Program : access // Inherit access class
   {
     static void Main(string[] args)
      {
        Program p=new Program();
        Console.Write("Enter your name:\t");
        p.name = Console.ReadLine(); // No Error!!
        p.print();
        Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result:

Output:
Enter your name: Rejin
My name is Rejin

Internal
  1. The internal access modifiers can access within the program that contain its declarations and also access within the same assembly level but not from another assembly.
  2. The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace.
  3. The variable or classes that are declared with internal can be access by any member within application. It is the default access specifiers for a class in C# programming.
Accessibility:
In same assembly (public)
  • Can be accessed by objects of the class
  • Can be accessed by derived classes
In other assembly (internal)
  • Cannot be accessed by object
  • Cannot be accessed by derived classes
Example:

namespace Internal_Access_Specifier
    {
        class access
        {
            // String Variable declared as internal
            internal string name;
            public void print()
            {
                Console.WriteLine("\nMy name is " + name);
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                access objaccess = new access();
                Console.Write("Enter your name:\t");
                // Accepting value in internal variable
                objaccess.name = Console.ReadLine();
                objaccess.print();
                Console.ReadLine();
            }
        }
    }

When the above code is compiled and executed, it produces the following result:

Output:
Enter your name: Rejin
My name is Rejin

protected internal
  1. Protected internal is the same access levels of both protected and internal.
  2. It can access anywhere in the same assembly and in the same class also the classes inherited from the same class.
  3. The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application.
  4. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.
Example:

namespace Internal_Access_Specifier
    {
        class access
        {
            // String Variable declared as protected internal
            protected internal string name;
            public void print()
            {
                Console.WriteLine("\nMy name is " + name);
            }
        }


        class Program
        {
            static void Main(string[] args)
            {
                access objaccess = new access();
                Console.Write("Enter your name:\t");
                // Accepting value in protected internal variable
                objaccess.name = Console.ReadLine();
                objaccess.print();
                Console.ReadLine();
            }
        }
    }

When the above code is compiled and executed, it produces the following result:

Output:
Enter your name: Rejin
My name is Rejin

I hope this page will helps to learn Access Specifiers in C#. Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Nov 21 2017 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: