Serving Information Simply

Sunday 14 July 2013

Properties in c#


     Properties can be get and set values. The C# language provides them as a well-situated way to make things easier syntax. They are implemented as methods in the intermediate language. They are regular access points to a class from external code. Unlike fields, properties do not indicate storage locations. Instead, properties have accessors that identify the statements to be executed when their values are read or written.

-          These are Special procedures that looks like fields but internally a method

-          It allows to put a check on values entered to some field

-          Here we Use a value keyword to input the value inside a property.

Syntax 1

<scope> <return type> <property name>
{
                set
                {
                                fieldname=value;
}             
get
{
                return fieldname;
}
}

Syntax 2

<scope> <return type> <property name>
{
                set;get;
}


Properties can of three types
1.     read/write property (set,get)
2.     read only property (get)
3.     write only property (set)

Example

Create a class Employee having fields empid,name,basic. Use properties to manage data and return Salary.
Note: Whenever we use the values, best is Property

using System;
class Employee
{
    //fields
    int empid;
    string name;
    int basic;
    public int EmpId //property
    {
        set
        {
            empid = value;
        }
        get
        {
            return empid;
        }
    }

    public string Name
    {
        set
        {
            name = value;
        }
        get
        {
            return name;
        }

    }
    public int Basic
    {
        set
        {
            if (value < 5000)
                throw new Exception("Sorry! Basic can never be lesser than 5000");
            basic = value;
        }
        get
        {
            return basic;
        }
    }
    public double Salary
    {
        get
        {
            return basic * 2.5;
        }
    }

}

class PropertyTest
{
    public static void Main()
    {
        Employee e = new Employee();
        e.EmpId = 67;
        e.Name = "Abhinav Bajpai";
        e.Basic = 8000;
        Console.WriteLine("Salary is {0}", e.Salary);
    }
}

Example : Use properties without any field name

using System;
namespace PropCheck
{
    class Employee
    {
        public int EmpId //property
        {
            set;
            get;
        }

        public string Name
        {
            set;
            get;
        }
        public int Basic
        {
            set;
            get;

        }
        public double Salary
        {
            get
            {
                return Basic * 2.5;
            }
        }

    }

    class PropertyTest
    {
        public static void Main()
        {
            Employee e = new Employee();
            e.EmpId = 67;
            e.Name = "Abhinav Bajpai";
            e.Basic = 8000;
            Console.WriteLine("Salary is {0}", e.Salary);
        }
    }
}
summary
We sharp out two customs to use properties. Properties are a influential way to put back methods and present a more perceptive way to use your objects. They are a grouping of fields and methods on a conceptual level.

---------------------------------------------------------------------------------

Thank you guys... keep visiting …. :)
If you like this post then please join this site and like it’s Facebook page for getting updates and contribute into the problem solving of members of this blog. You can also post your issues on this blog's Facebook page and can get the solutions.