Friday, 18 March 2016

Access Modifiers

Following are the types of access modifiers for Classes and their Members:

A)Private: Class can not be declare as private access modifier but members can. When a member (method, properties, variables etc.) of a class declared using a private access modifier, that member i'll be accessible only with in that particular class. By default all members are private when no access modifier is defined.

B)Public: Both, class and its members can be defined with public access modifier. Class declare with public access modifier, accessible from any where, with in project or with in solution. Note to access public classes with in other project's, firstly, we must to have add a reference of a project containing that particular class to the project where we want to use this class.

C)Internal: Accessible within the same assembly (an assembly is may be a project). Both, class and its members are only accessible with in the same project where they are declared. There is a compile time error saying "is inaccessible due to its protection level", when we want to use the things in other assembly (Project) that are declared with internal access modifier.

D)Protected: When a member of a class declared with a protected access modifier, it can be accessible any where but only for its child classes. Means that only that class can have access of protected member of other class when it inherit it inherit this class containing protected member.

E)Protected Internal: When a member declare with protected internal access modifier, it can be accessible anywhere for only child classes and for other classes with in same project/assembly.
Note: Classes and structs that are declared directly within a namespace (in other words that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.
The access level for class members and struct members, including nested classes and structs, is private by default.

Note: Class can only be internal or public and other access modifier can't be added with the class. Compile time error will be generated if we use private, protected or protected internal access modifier with the class. 

Compiler I'll generate following error, "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal".


Following examples are explain the access modifiers:


1.Class is default to internal access if no access modifier is defined;
    class PrivateClass
     {
     }
2.Class can never be private, protected, or protected internal;
//Error: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
    //private class PrivateClass
    //{
    //}

3.Class Members Access Levels with in the same assembly/project but in different class;
public class MySampleClass
    {
        //Members are default to private if no access modifier is defined.
        int myPrivate;
        public int myPublicMember;
        protected int myProtectedMember;
        protected internal int myProctedInternalMember;
        internal int myInternalMember;

    }
Members Access In Same Project Different Class
Here we can see that, we can access all members of  'MySampleClass' accept its protected and private members as well in different class (APP1_Program).

4.Class Members Access Levels with in the same assembly/project and in 'Same Class' as well;
public class MySampleClass
    {
        int myPrivate;
        public int myPublicMember;
        protected int myProtectedMember;
        protected internal int myProctedInternalMember;
        internal int myInternalMember;

        public void MyMethod()
        {
        //Here we can see that, we can access all members of MySampleClass including its             private and protected members as well.       
            MySampleClass objMySampleClass = new MySampleClass();
           
        }
    }


Members Access In Same Project Same Class
Here we can see that, we can access all members of  'MySampleClass' including its protected and private members as well in same class (MySampleClass).

5.Class Members Access Levels with in the same assembly/project and in 'Child class'  as well;
public class MySampleClass
    {
        int myPrivate;
        public int myPublicMember;
        protected int myProtectedMember;
        protected internal int myProctedInternalMember;
        internal int myInternalMember;

        public void MyMethod()
        {
        //Here we can see that, we can access all members of MySampleClass       including its private and protected members as well.       
            MySampleClass objMySampleClass = new MySampleClass();           
        }
    }

//The MySampleClassChild is in same project of its parent class MySampleClass
  public class MySampleClassChild : MySampleClass
    {
        public void MyChilClassMethod()
        {
            //Here we can access all memebrs of MySampleClass (Parent Class) in                   MySampleChildClass with in same project.
            MySampleClassChild objMySampleClassChild = new MySampleClassChild();           
        }
    }
Members Access In Child Class In Same Project

Here we can see that, we can access all members of Parent Class 'MySampleClass' including its protected and internal members as well except private members of Parent Class 'MySampleClass' in same class (MySampleClass).

6.Class Members Access Levels in different assembly/project;
namespace Practice_2

{
    class APP2_Program
    {
        static void Main(string[] args)
        {
            //Here we can access only public member of MySampleClass
            MySampleClass objMySampleClass = new MySampleClass();           
        }
    }
}
Member Access Different Project Different Class
Here we can see that, we can access only public member of Class 'MySampleClass'  in different project.

7.Class Members Access Levels in different assembly/project for Child Class;
namespace Practice_2
{
    public class MySampleClassChild_APP2 : MySampleClass
    {

        public void MyChilClassMethod()
        {
            //Here we can access all members of Parent Class 'MySampleClass' in child class 'MySampleClassChild_APP2' in different project except private and internal members of parent class.
            MySampleClassChild_APP2 objMySampleClassChild_APP2 = new MySampleClassChild_APP2();                       
        }
       
    }
}
Members Access In Child Class Different Project

Here we can access all members of Parent Class 'MySampleClass' in child class 'MySampleClassChild_APP2' in different project except private and internal members of parent class.


(Practice Makes a Man Perfect)

No comments: