Wednesday, 29 April 2015

Access Modifiers in c# asp.net


Access modifiers are keywords used to specify the declared accessibility of a member or a type.

Why to use access modifiers?


Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features.


In C# here are 5 different types of Access Modifiers.













































public

The public keyword is an access modifier for types and type members. Public access is the most permissive access level.

There are no restrictions on accessing public members.

Accessibility: 
  1. Can be accessed by objects of the class
  2. Can be accessed by derived classes

 Example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;


namespace AccessModifier
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine ("Please enter first number");
            int a = int.Parse(Console.ReadLine());
            Console.WriteLine ("Please enter second number");
            int b = int.Parse(Console.ReadLine());
            MyTestA ts = new MyTestA ();
            // access to public member
            int sum = ts.getSum(a,b);
   
            Console.WriteLine ("Entered numbers are : {0}, {1}\n", a, b);
            Console.WriteLine ("Sum of numbers is : {0}\n", sum);
            Console.Read();
        }
    }

    Public class MyTestA
    {
        Private int sum;
        //public member
        Public int getSum(int a, int b)
        {
            this.sum = a + b;
            Return this.sum;
        }
     
    }
}


Output:-















private

Private access is the least permissive access level.

Private members are accessible only within the body of the class or the struct in which they are declared.

Accessibility: 
  1. Cannot be accessed by object
  2. Cannot be accessed by derived classes

Here the private member  <<  ts.getMultiplication(a, b);  >> not accessable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;


namespace AccessModifier
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine ("Please enter first number");
            int a = int.Parse(Console.ReadLine());
            Console.WriteLine ("Please enter second number");
            int b = int.Parse(Console.ReadLine());
            MyTestA ts = new MyTestA();
            // access to public
            Int sum = ts.getSum(a,b);
            // Access to private member
            Int multi = ts.getMultiplication(a, b);
            Console.WriteLine ("Entered numbers are : {0}, {1}\n", a, b);
            Console.WriteLine ("Sum of numbers is : {0}\n", sum);
            Console.WriteLine ("multiplication of numbers is : {0}\n", multi);
            Console.Read ();
        }
    }

    Public class MyTestA
    {
        Private int sum;
        //public member
        Public int getSum (int a, int b)
        {
            this.sum = a + b;
            return this.sum;
        }
       //private member
        Private Int getMultiplication (int a, int b)
        {
            this.sum = a + b;
            Return this.sum;
        }
    }
}


See Error in Image below
The above program will give compilation error, as access to private is not permissible

























protected

A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.

Accessibility: 
  1. Cannot be accessed by object
  2. By derived classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;


namespace AccessModifier
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine ("Please enter first number");
            int a = int.Parse(Console.ReadLine());
            Console.WriteLine ("Please enter second number");
            int b = int.Parse(Console.ReadLine());
            Console.WriteLine ("Entered numbers are : {0}, {1}\n", a, b);
            MyTestB ts = new MyTestB();
            // access to public class MyTestB
            int sum = ts.getSum(a, b);
            Console.WriteLine ("sum of numbers are : {0}\n", sum);
            Console.ReadKey ();
        }
    }

    Public class MyTestA               
    {
        int sum;
       //protected member
        Protected int getMultiplication(int a, int b)
        {
            this.sum = a * b;
            Return this.sum;
        }
    }
    Public class MyTestB : MyTestA
    {
        Private int sum;
        //public member
        Public int getSum(int a, int b)
        {
            // Access to protected member of base class MyTestA
            int multi = this.getMultiplication(a,b);
            Console.WriteLine ("multiplication of numbers is : {0}\n", multi);

            // now retrun the sum
            this.sum = a + b;
            return this.sum;
           
        }
      
    }
}

Access of protected member in main it is not available

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;


namespace AccessModifier
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine ("Please enter first number");
            int a = int.Parse(Console.ReadLine());
            Console.WriteLine ("Please enter second number");
            int b = int.Parse(Console.ReadLine());
            Console.WriteLine ("Entered numbers are : {0}, {1}\n", a, b);
            MyTestB ts = new MyTestB ();
            // access to public class MyTestB
            int sum = ts.getSum(a, b);
            Console.WriteLine ("sum of numbers are : {0}\n", sum);

            // Access to protected member of class MyTestA in Main Function
    Error Line >>        int multi = ts.getMultiplication(a, b);
            Console.WriteLine ("sum of numbers are : {0}\n", sum);
            Console.ReadKey ();
        }
    }

    Public class MyTestA
    {
        int sum;
       //protected member
        Protected int getMultiplication (int a, int b)
        {
            this.sum = a * b;
            return this.sum;
        }
    }
    Public class MyTestB: MyTestA
    {
        Private int sum;
        //public member
        Public int getSum (int a, int b)
        {
            // Access to protected member of base class MyTestA
            int multi = this.getMultiplication(a,b);
            Console.WriteLine ("multiplication of numbers is: {0}\n", multi);

            // now retrun the sum
            this.sum = a + b;
            Return this.sum;
           
        }
      
    }
}

In the above program we try to access protected member in main it is not available as shown in the picture below that num1 is not listed in intellisense.




















Internal

The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll).

In other words, access is limited exclusively to classes defined within the current project assembly.

Accessibility:

In same assembly (public) 
  1. Can be accessed by objects of the class
  2. Can be accessed by derived classes
In other assembly (internal) 
  1. Cannot be accessed by object
  2. Cannot be accessed by derived classes
protected internal

The protected internal accessibility means protected OR internal, not protected AND internal.

In other words, a protected internal member is accessible from any class in the same assembly, including derived classes.

The protected internal access modifier seems to be a confusing but is a union of protected and internal in terms of providing access but not restricting. It allows:  
  1. Inherited types, even though they belong to a different assembly, have access to the protected internal members.
  2. Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.
Default access

A default access level is used if no access modifier is specified in a member declaration. The following list defines the default access modifier for certain C# types:

enum: The default and only access modifier supported is public.

class: The default access for a class is private. It may be explicitly defined using any of the access modifiers.

interface: The default and only access modifier supported is public.

struct: The default access is private with public and internal supported as well.

The default access may suffice for a given situation, but you should specify the access modifier you want to use to ensure proper application behavior.

Note: Interface and enumeration members are always public and no access modifiers are allowed.

Conclusion

I hope that this article would have helped you in understanding accessibility modifiers. Your feedback and constructive contributions are welcome.





No comments:

Post a Comment