Monday, March 31, 2014

Access Modifiers

In this article am going to discuss about access modifiers and their functionality along with, how they differ from each other and what are the basics of all these.
I’ll include all 4 access modifiers in this article.

Access Modifiers in C#

·         By default, data and methods defined in a class can only be used inside that class itself.
·         We must choose to expose them.
·         Access modifiers let you specify how programs that use your classes can access their data and methods.

 Class Access Modifiers | C#

Am going to explain this through an example-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program

    {
       static void Main(string[] args)

                public int Value;
      // public access modifier

                private string Message;
      // private access modifier

                protected void function();
      // protected access modifier


        }
}



We can use any of the class modifiers in our class but working mechanism of all these modifiers are different.


Modifiers | Types

Public

PUBLIC modifier is used at that place in the class of which values, attributes can be easily accessed by the any other object.

Private

PRIVATE access modifier is used where we want the functionality of attribute of that class can be only used within that class only.

Protected

-          It is used where the class member can only be accessed within the same class.
-          Inherited class of that class(child/subclass)

 Internal

-         -    It is the advanced functionality of C# 5.0 or above.
-         -    In this modifier class member can be accessed by any class in the same assembly.
-         -    Package of code like a library or other program.

Example

There are 2 parts in this code; first part is representing construction building and access modifiers definitions. In the second code there is a calling of the access modifiers functionalities.

Part: 1 | Code

// Book.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello_Word
{
    class Book

    {
        // access modifier- PUBLIC
        public string Name;
        public decimal Price;
        public string Description;

        // access modifier- PRIVATE
        private decimal discount;

        // public constructer
        public Book(string bookName, decimal price)      

        {
            Name = bookName;
            Price = price;
            discount = 0.0m;
        }
    }
}

Part: 2 | Code

    class Program
    {
        static void Main(string[] args)
        {

            Book b1 = new Book("Black Book", 1050.00m);
            Book b2 = new Book("Complete Refrence", 1250.00m);

            string str = b1.Name;

          }
     }


When you do try to define b1.discount there will be no option in the list as




But if you do try to declare, it will show an error like this


// Program.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program
    {
        static void Main(string[] args)
        {

            Book b1 = new Book("Black Book", 1050.00m);
            Book b2 = new Book("Complete Refrence", 1250.00m);

            string str = b1.Name;

            decimal dsc = b2.discount;
        }
     }
}


// We can not directly use discount in our next file, because it is defined under a private class, we didn’t get the options from the list but if we do try to do so, it will surely show an error-
“Inaccessible due to private modifier”



Same in the case of protected modifier, if you try to access its properties it will again show the same message-



___________________________________________________________

For more technical blogging stuffs please visit,
http://www.c-sharpcorner.com/authors/2072a9/abhishek-jaiswal.aspx

___________________________________________________________

No comments:

Post a Comment