Thursday, 11 September 2014

Access modifiers in C#

Today i am going to explain what is access modifiers in C#.


One of the most  favorite question of interviewer.  
What is modifiers?
Different types of modifiers  and different between them  ?

Access modifiers
Access modifiers are keywords used to specify the declared accessibility of a member or a type.
In C# there are 5 different types of Access Modifiers.

Public
-No restrictions to access
-The type or member can be accessed by any other code in the same assembly or another assembly that references it.

protected

-Access is limited to within the class definition and any class that inherits from the class.
-The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal
-Access is limited exclusively to classes defined within the current project assembly.
-The type or member can be accessed only by code in the same class or struct.

private
-Access is limited to within the class definition; This is the default access modifier type if none is formally specified.
-The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
protected internal member enjoy dual effect.it is accessible anywhere in the project as well as if that class is inherit one other class and that is in other project then only it can be accessible to other project also.

Go to live Example here.

Point to be notice  here
we can access these member objpublic objinternal and  objprotectedinternal in program class we can not access the private member outside the class and we can also not  access the protected member with derived class. In Order to access the protected member with derived class we have to create the object of derived  class.
I have create the object of Program class.that object can access the protected  member. because protected definition saying "It can be access with his derived class"
many of developer got confused in protected modifiers just because we have to create  object of derived  class.

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

Please find the code below.
class Fristclass
    {
        public int objpublic;
        private int objprivate;
        protected int objprotected;
        internal int objinternal;
        protected internal int objprotectedinternal;

        void show()
        {
            Console.WriteLine(objprivate); // private can access inside  same class or struct
        }
    }
    class Program:Fristclass
    {
        static void Main(string[] args)
        {
            Fristclass objclass = new Fristclass();
            Console.WriteLine(objclass.objinternal); // it can be current project assembly and same class and struct also
            Console.WriteLine(objclass.objprotectedinternal); // one other class and that is in other project then only it can be accessible to other project also.
            Console.WriteLine(objclass.objpublic); // no limit of access 



            // for protected and
            Program programclassobj = new Program();
            Console.WriteLine(programclassobj.objprotected);  // It can be access with his drived class
            Console.WriteLine(programclassobj.objprotectedinternal); // one other class and that is in other project then only it can be accessible to other project also
        }
    }

Happy coding....

No comments:

Post a Comment