Properties in C#

Properties are used to encapsulate the state of an object in a class. This is done by creating Read Only, Write Only or Read Write properties. Traditionally, methods were used to do this. But now the same can be done smoothly & efficiently with the help of properties. 

A property with Get() can be read & a property with Set() can be written. Using both Get() & Set() make a property Read-Write. A property usually manipulates a private variable of the class. This variable stores the value of the property. A benefit here is that minor changes to the variable inside the class can be effectively managed. For example, if we have earlier set an ID property to integer and at a later stage we find that alphanumeric characters are to be supported as well then we can very quickly change the private variable to a string type.

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

namespace CreateProperties
{
    class Properties
    {
        //Please note that variables are declared private
        //which is a better choice vs declaring them as public

        private int p_id = -1;

        public int ID
        {
            get
            {
                return p_id;
            }
            set
            {
                p_id = value;
            }
        }

        private string m_name = string.Empty;

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

        private string m_Purpose = string.Empty;

        public string P_Name
        {
            get
            {
                return m_Purpose;
            }
            set
            {
                m_Purpose = value;
            }
        }
    }
}


using System;

namespace CreateProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            Properties object1 = new Properties();

            //Set All Properties One by One
            object1.ID = 1;
            object1.Name = "www.code-kings.blogspot.com";
            object1.P_Name = "Teach C#";

            Console.WriteLine("ID   " + object1.ID.ToString());

            Console.WriteLine("\nName   " + object1.Name);

            Console.WriteLine("\nPurpose   " + object1.P_Name);

            Console.ReadKey();
        }
    }
}




Also, properties can be used without defining a a variable to work with in the beginning. We can simply use get & set without using the return keyword i.e. without explicitly referring to another previously declared variable. These are Auto-Implement properties. The get & set keywords are immediately written after declaration of the variable in brackets. Thus, the property name & variable name are the same.

using System;
using System.Collections.Generic;


using System.Text;
 
public class School
{
    public int No_Of_Students{ get; set; } 
    public string Teacher{ get; set; } 
    public string Student{ get; set; }
    public string Accountant{ get; set; } 
    public string Manager{ get; set; } 
    public string Principal{ get; set; }
}


Please Note :

** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.0 but code should be very similar for previous versions of .Net
** All Program Codes on Blog are  100%  tested & running.