Monday, 5 March 2018

020 C # Property

Properties 
Properties  are the some special statement of a class to expose the member of a class to the outside world . Properties accessors contain get and set keyword  in declaration syntax . get accessors take the value from outside world and set accessors , set the value to class member.

public class Student
{
         //private veriable
           private int mAge; 

         //property declaration
         public int Age
        {
            get
                 {
                   return mAge;
                }
         set
             {
              mAge = value;
           }
      }
}


//Class for Demo
public class Demo
{
           private void Show()
           {
               Student obj = new Student();
              obj.Age = 12;//Property Assingmnent
          }
}
          Generally private member variable can not be access from outside of the class. To assign value to the private member variable and to retrieve the value from private member variable , get set method is generally used. Another advantage of property is, during get and set property declaration, customized validation can be done to allow only valid values. Here is the example of a property.

public class Student
{
             //private veriable
             private int mAge;
            //property declaration
             public int Age
             {
             get
                {
                  return mAge;
               }
            set
           {
              if (value < 18)
              {
               Console.WriteLine("You have now attained the age");
              }
              else
            {
             mAge = value;
           }
         }
    }
}


//Class for Demo
public class Demo
{
            private void Show()
           {
               Student obj = new Student();
              obj.Age = 12;//Property Assingmnent
            }
}


          Property value can be set, by the keyword value. value is a keyword which can assign any type of value like string, integer, Boolean, collection to the property . The value keyword take the value from get property and assign it to the class member variable. During the get and set  , checking can be done and additional required statement can be written to validate the value. Here is the example of property with additional checking.

Read only and Write only Property : Sometimes we come with situation, once an object instance is created , the value cannot be changed.You can declare such property with some additional statement .

Read only Property : Read only property means, when object instance is created, the property will return the value of the member only. get set keyword is used in property, but in read only  get is used, set portion not written here. Where is the example of read only property.

public class Student
{
           //private veriable
            private readonly int mAge;
          //property declaration
           public int Age
          {
            get
              {
             return mAge;
           }
        }
}


//Class for frmo
public class Demo
{ 
          private void Show()
       {
         Student obj = new Student();
          int lAge = obj.Age;//Get Age from Student class
       }
}


Write only Properties : A property can be define such a way that only assignment can be done that is Write only. In this case set portion of the property written and get portion of the property omitted. Here is the example of write only property.
  
public class Student
{
             //private veriable
             private int mAge;

           //property declaration
           public int Age
           {
              set
                 {
                   mAge = value;
                }
      
}

//Class for Demo
public class Demo
{
          private void Show()
         {
              Student obj = new Student();
             obj.Age = 12;//Property Assingmnent
       }
}

Indexer : Indexer is a special kind of property that allow a group of object outside the world. this is the keyword used indexer. This keyword tails the compiler that the object will it self access the property. Increment get and set property and accessor[] operator. Indexer can be overloaded and induction always use this keyword and always refer to a group of element. Here is the example of indexer.

public class Student
{
         //private veriable
         private int[] mAge = new int[10]; 

        //property declaration
       public int this[int i]
       {
           get
               {
                   return mAge[i];
              }
        set
           {
              mAge[i] = value;
         }
    }
}


//Class for Demo
public class Demo
{
               private void Show()
              {
               Student obj = new Student();
              obj[0] = 12;
              obj[1] = 12;
              obj[3] = 13;
            }
}

Indexer can be Overloaded : An indexer is not required to be integer and fixed. Indexer can have multiple parameter of different type, here is the example indexer overloading.
 
public class Student
{
      //private veriable
      private int[] mAge = new int[10];
     //property declaration
     public int this[int i]
     {
            get
                  {
                   return mAge[i];
                }
        set
            {
                mAge[i] = value;
            }
        }
           public string this[string Address]
          {
              get
            {
                      return Address;
                  }
            set
                {
                 //statement
               }
         }
}


//Class for Demo
public class Demo
{
           private void Show()
          {
              Student obj = new Student();
              obj[0] = 12;
             obj[1] = 12;
              obj[3] = 13;
         }
}


Collection Property : When a group of similar type object is used to assign private member of a class or retrieve the same, collection property is used. Namespace System.Collections need to be added in the top of a program, when we are using a collection property. Depending upon the type of collection program can be changed for get and set property. Here is the example of collection property.

 
public class Student
      {
         //private veriable
        private ArrayList mAge = new ArrayList();

        //property declaration
        public ArrayList Age
             {
                set
                  {
                 Age = value;
              }
         }
}

//Class for Demo
public class Demo
{
           private void Show()
         {
           Student obj = new Student();
           ArrayList arryList1 = new ArrayList();
           arryList1.Add(1);
          arryList1.Add("Two");
           arryList1.Add(3);
           arryList1.Add(4.5);
          obj.Age = arryList1;
        }
}

Abstract properties : An abstract class have some properties and if it is derived , the abstract class properties will be inherited to the derived class. You will get all properties of abstract class in the derived class.

abstract class Student
{
           //private veriable
           private ArrayList mAge = new ArrayList(); 

         //property declaration
          public ArrayList Age
         {
             set
                 {
                 Age = value;
               }
       }
}

//Inherited class
class Class : Student
{
}


//Class for Demo
public class Demo
{
       private void Show()
       {
           Class obj = new Class();
           ArrayList arryList1 = new ArrayList();
          arryList1.Add(1);
           arryList1.Add("Two");
          arryList1.Add(3);
          arryList1.Add(4.5);
          obj.Age = arryList1;
       }
}



No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...