Search This Blog

Thursday 3 December 2015

Access Modifiers in C#

The public Access Specifier
The public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any member that is declared public can be accessed from outside the class.

using System;

namespace ConsoleApplication1
{
    class Car
    {
        // "CarColor" variable is private, it cannot be accessed outside the class definition
        private string CarColor;
    }
    class Bike
    {
        // "BikeColor" variable is public, it can be accessed outside the class definition
        public string BikeColor;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.CarColor = "Red"// Error: "CarColor" is inaccessible due to its protection level

            Bike bike = new Bike();
            bike.BikeColor = "Red";

            Console.ReadLine();
        }
    }
}

The private Access Specifier
The private access specifier allows a class to hide its member variable & member functions from other class and functions. Therefore, the private member of a class is not visible outside a class. If a member is declared private, only the functions of that class can access the member. Even the instance of the class cannot access its private Members. Therefore, the data is hidden and cannot be altered any function other than the member functions of the class.

using System;

namespace ConsoleApplication1
{
    class Car
    {
        // "Model" variable is private, it cannot be accessed outside the class definition
        private string Model;

        void Honk()
        {
            Console.WriteLine("PARRP PARRP!");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.Model = "Maruti Swift"// Error: "Model" is inaccessible due to its protection level
            car.Honk(); // Error: "Honk()" is inaccessible due to its protection level


            Console.ReadLine();
        }
    }
}

Tips:

When you do not specify any data member as public, protected, or private, then the default access specifier for a data member is private. In the following example, the data member Model is private, even though it has not been specified explicitly.
class Car
{
    string Model;
}

The protected Access Specifier
This specifier allows a class to hide its member variables and member functions from other class objects and functions, except the child class. The protected access specifier becomes important while implementing inheritance.

using System;

namespace ConsoleApplication1
{
    class Car
    {
        // "Model" variable is protected, it cannot be accessed outside the class definition, except the child class
        protected string Model;

        void Honk()
        {
            Console.WriteLine("PARRP PARRP!");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.Model = "Maruti Swift"// Error: "Model" is inaccessible due to its protection level
            car.Honk(); // Error: "Honk()" is inaccessible due to its protection level


            Console.ReadLine();
        }
    }
}

Accessing the protected variable in the child class

using System;

namespace ConsoleApplication2
{
    class Car
    {
        // "Model" variable is protected, it cannot be accessed outside the class definition, except the child class
        protected string Model;

        void Honk()
        {
            Console.WriteLine("PARRP PARRP!");
        }
    }
    class Program : Car
    {
        public void SetModel()
        {
            Model = "Maruti Swift";
        }
        public void GetModel()
        {
            Console.WriteLine(Model);
        }
        static void Main(string[] args)
        {
            Car car = new Car();
            car.Model = "Maruti Swift"// Error: "Model" is inaccessible due to its protection level
            car.Honk(); // Error: "Honk()" is inaccessible due to its protection level

            Program program = new Program();
            program.SetModel();
            program.GetModel();


            Console.ReadLine();
        }
    }
}

Tips:

When you do not specify any data member as public, protected, or private, then the default access specifier for a data member is private. In the following example, the data member Honk() is private, even though it has not been specified explicitly.
class Car
{
    void Honk()
    {
           
    }
}

The infernal Access Specifier
The internal access specifier allows a class to expose its member variables and member functions to other functions and objects. Any member that is declared internal can be accessed from any class or method defined within the application in which the member is defined. When you do not specify any data member as public, protected or private the default access specifier for a class is internal.

using System;

namespace ConsoleApplication1
{
    class Car
    {
        // "CarColor" variable is private, it cannot be accessed outside the class definition
        private string CarColor;

        // "Honk()" is internal, it cannot be accessed outside the class definition
        internal void Honk()
        {
            Console.WriteLine("PARRP PARRP!");
        }

    }
    class Bike
    {
        // "BikeColor" variable is internal, it can be accessed outside the class definition
        internal string BikeColor;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.CarColor = "Red"// Error: "CarColor" is inaccessible due to its protection level
            car.Honk();

            Bike bike = new Bike();
            bike.BikeColor = "Red";

            Console.ReadLine();
        }
    }
}

Tips:

When you do not specify any data member as public, protected or private the default access specifier for a class is internal.

The protected internal Access Specifier
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. The protected internal access specifier becomes important while implementing inheritance.


Public
Public Members are available anywhere.
Private
Private Members are available only within the same class.
Protected
Protected Members are available within the same class and derived class
Internal
Internal Members are available anywhere within the same assembly
Protected Internal
Protected Internal are available anywhere within the same assembly and derived class in any another assembly.


No comments:

Post a Comment