Monday 30 January 2017

ACCESS MODIFIERS IN C#

ACCESS MODIFIERS IN C#



Types Vs Type Members


In C# there are 5 different access modifiers in c#
1.Private
2.Protected
3.Internal
4. Protected Internal
5. Public

Private members are available only with in the containing type, where as public members are available any where. There is no restricction.

Protected Members are available, with in the containing type and to the types that derive from the containing type

Access Modifier                       Accessibility
Private ->                  Only with in the containing class
Public ->                  Any where, No Restrictions
Protected ->                 With in the containing types and the types derived from the
                                                 containing type.
-------------------------------------------------------------------------------------------------------------------------

using System;
using System.IO;

namespace ConsoleApplication4
{

    //public class Customer
    //{

    //    private int _id;

    //    public int Id
    //    {
    //        get { return _id; }
    //        set { _id = value; }

    //    }
    //}

    public class Customer
    {
        protected int _id;
    }

    public class corporateCustomer : Customer
    {
        public void printID()
        {
            corporateCustomer cc = new corporateCustomer();
            cc._id = 101;
        }
    }

   public  class Program
    {
        public static void Main()
    {
        Customer C1 = new Customer();
        Console.WriteLine(C1._id);
    }

    }
}
-------------------------------------------------------------------------------------------------------------------------

INTERNAL AND PROTECTED INTERNAL ACCESS MODIFIERS IN C#




INTERNAL AND PROTECTED INTERNAL

A member with internal access modifier is available anywhere with in the containing assembly. it's a compile time error to access, an internal member from outside the containing assembly.

Protected Internal members can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. It is a combination of protected and internal. If you have understood protected and internal, this should be very easy to follow. 



Access Modifier                       Accessibility
Private                    Only with in the containing class
Public                   Any where, No Restrictions
Protected                   With in the containing types and the types derived from the 
                                                 containing type.
Internal                  Anywhere with in the containing assembly
ProtectedInternal         Anywhere with in the containing assembly, and from within a 

                                                 derived class in any another assembly.





INTERNAL:- 
Internal fields are accessible within the assembly where it is defined not outside of the assembly.

PROTECTED INTERNAL:-
Protected Internal are accessible within the assembly and in the derived class through inheritance within another assembly(refering the base class in the derived class through reference or namespace declaration).

No comments:

Post a Comment

Note: only a member of this blog may post a comment.