Access Specifier example

For this example, We create 2 Class library named AccessSpecifier_Demo AND UsingAccessSpecifier. We will declare variables with all access specifiers in class AccessSpecifier_Declaration and will use the dll that is created into other class AccessSpecifier_UseInDifferentAssembly which is in another class library.

For creating class library
1. Open Visual studio
2. Click on New Menu => Projects
3. Select Visual C# option from the New Project popup.
4. Select Class Library option from right pane And name it as AccessSpecifier_Demo.
5. Rename the Class1.cs file name as AccessSpecifier_Declaration.
6. Also declare class AccessSpecifier_UseInSameAssembly in same Class library below AccessSpecifier_Declaration class.

Below is the code for class library.

+
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
namespace AccessSpecifier_Demo
{
    public class AccessSpecifier_Declaration
    {
        public int i;
        private int j;
        protected int k;
        internal int m;
        protected internal int n;
    }
 
    public class AccessSpecifier_UseInSameAssembly
    {
        AccessSpecifier_Declaration accessSpec = new AccessSpecifier_Declaration();
        
        void setValues()
        {
            accessSpec.i = 1;
            accessSpec.m = 2;
            accessSpec.n = 3;
        }
    }
}

Create Class library with name UsingAccessSpecifiers. Follow same procedure mentioned above.
Code for this class library as below.

+
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AccessSpecifier_Demo;
 
namespace UsingAccessSspecifier
{
    public class AccessSpecifier_UseInDifferentAssembly
    {
        
        public void setValues()
        {
             AccessSpecifier_Declaration accessSpec = new AccessSpecifier_Declaration();
             public void setValues()
             {
                  accessSpec.i = 0;
             }            
        }
    }
}

Now first create object of class AccessSpecifier_Declaration as accessSpec and create one function setValues() in class AccessSpecifier_UseInSameAssembly to assign values to the variables from AccessSpecifier_Declaration class. when you try to access the variables using object accessSpec you will see that as per access specifiers definition you can able to access only 3 variables - public, Internal, Protected Internal in a class which is present in same assembly(See below screenshot. you will see only 3 variables available in intellisense list marked as red).

Access specifier in C# 1

Now use same class AccessSpecifier_UseInSameAssembly which derives functionality of base class AccessSpecifier_Declaration.
and try access all the variables directly. You will notice that you can able to access all variables and not the private variable. see below screenshot for the same. You can see the error when you try to access private variable.

Access specifier in C# 2

Now add reference of class library AccessSpecifier_Demo in another class library UsingAccessSpecifier and create object of AccessSpecifier_Declaration in AccessSpecifier_UseInDifferentAssembly class and try to access all variables using this object instance. You will notice that as per access specifier definition, you can access only a public variable. See below example screenshot for the same.

Access specifier in C# 3

Now instead of using object of AccessSpecifier_Declaration, use AccessSpecifier_UseInDifferentAssembly to derive from class AccessSpecifier_Declaration and try to access all variables of this class in setValues function, you will see that you can access only Public, Protected and Protected Internal variables. See below example screenshot.

Access specifier in C# 4

No comments :