Wednesday, May 29, 2013

get and set property in C#

What is property ?

We all know C# is a object oriented programming language. And in OOP we are familiar with a great feature called “Encapsulation”. In c# concept of property gives us same flavor . Basically using property we can very easily set and get value of class member. It gives us real time picture of data binding with class object.
Types of property:
Basically property are two types.
Get property:
Using Get property we can get the value of data member of class. The syntax of get property is
get{
                return Membername
      }
Set property
Using set property we can set value of class member .The syntax of set property is
Set
{
Membername=value
}

Example of get and set property

class MyFriend
    {
        private string FriendName;
        private string Street;

        public string P_FirstName
        {
            get{return FriendName;}
            set { FriendName = value; }
        }

        public string P_Street
        {
            get{return Street;}
            set { Street = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyFriend objF = new MyFriend();
            objF.P_FirstName = "Sourav Kayal";   //Setting value in property of FirstName Data member
            objF.P_Street = "Kolkata";           //Setting value in property of Street Data member
            Console.Write("Name:"+ objF.P_FirstName + " Place:" + objF.P_Street); //Geting value of Data Member using property
            Console.ReadLine();
        }
    }


In C# we can  automatically implemented property. This feature indicates to the C# compiler that a hidden field be generated and used as the backing store. Then, the get and set statements are expanded to use that hidden field.

   class MyFriend
    {
        public string FriendName { get; set; }  //Created data member with propertys
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyFriend objF = new MyFriend();
            objF.FriendName = "sourav Kayal";  //Set data member using property
            Console.Write(objF.FriendName);    //Fetching data value using property
            Console.ReadLine();
        }
    }



 Write Validation code using property

We can write validation code within property. In set property before setting value to class member we can implement our business validation logic. Here I have given a example of  business validation using property.

class MyFriend
    {
        private string MobileNumber;
        public string p_MobileNumber
        {
            get {return MobileNumber ; }
            set {
                    if (value.Length < 10)
                        throw new Exception("Please enter 10 digit mobile number");
                    else
                    {
                        MobileNumber = value;
                    }
                }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyFriend objF = new MyFriend();
            try
            {
                objF.p_MobileNumber = "12345";
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            Console.ReadLine();
        }
    }




No comments:

Post a Comment