Table Of Content

C# Properties Tutorial

1- C# Property

The property is a member of a class, interface. It is an extension of a field. Property allows you access to a field or change the value of that field, without the need for direct access into field.

You can create a Property only allows access to a field, not allowed to change the value of the field, and vice versa. This is the most potent of the Property.

With the field, if you can access it from the outside, you can also change the value of it, this is obviously dangerous, consider this example:
Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPropertyTutorial
{
  class Employee
  {
      // To be accessible from outside, this field must be public or protected.
      public string code;

      // To be accessible from outside, this field must be public or protected.
      public string name;

      public Employee(string code, string name)
      {
          this.code = code;
          this.name = name;
      }
  }


  class EmployeeTest
  {

      public static void Main(string[] args)
      {
          // Create Employee object
          Employee john = new Employee("E01", "John");

          // You can access the name of the employee
          // (name is a public field so you can access from outside).
          Console.WriteLine("Employee Name = " + john.name);

          // However, you can also assign a new value to the name field.
          // (This is obviously dangerous).
          john.name = "Marry";

          Console.WriteLine("Employee Name = " + john.name);

          Console.Read();
      }
  }

}
Property is a solution to solving the problem mentioned above. Here is an example in which uses the Code, Name Property  to access the code, name of the   Employee2 class.
Employee2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPropertyTutorial
{
    class Employee2
    {
         // This field is private, does not allow access from outside.
        private string code;

        // This field is private, does not allow access from outside..
        private string name;


        // Declare a property, which is public, accessible from outside.
        public string Code
        {
            get
            {
                return this.code;
            }
            set
            {
                // value is a special keyword,
                // It implies the new value assigned to the field.
                this.code = value;
            }
        }

        // Declare a property, which is public, accessible from outside.
        // not allowed to assign new value.
        public string Name
        {
            get
            {
                return this.name;
            }
        }



        public Employee2(string code, string name)
        {
            this.code = code;
            this.name = name;
        }
    }

}
Test:
Employee2Test.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPropertyTutorial
{
    class Employee2Test
    {
        public static void Main(string[] args)
        {

            // Create Employee object.
            Employee2 john = new Employee2("E01", "John");

          
            Console.WriteLine("Employee Code = " + john.Code);
            Console.WriteLine("Employee Name = " + john.Name);
            Console.WriteLine("-------");

            // Assign a new value to the Code property.
            john.Code = "E02";

            // Can not assign new value to Name.
            // john.Name = "Marry";

            Console.WriteLine("Employee Code = " + john.Code);
            Console.WriteLine("Employee Name = " + john.Name);

            Console.Read();
        }
    }

}
Run the example:

2- Abstract Property

Property uses to get and set the value of a field, in essence it is considered as a special method, so it can also be declared abstract  and it will be implemented in a subclass. Class have property declared as abstract, it must be declared as abstract. The abstract properties can also be declared in the interface.
Animal is a class that declares two abstract properties (Name & Age):
Animal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPropertyTutorial
{
    abstract class Animal
    {

        // An abstract property
        public abstract string Name
        {
            get;
        }

        // An abstract property has both set & get
        public abstract int Age
        {
            get;
            set;
        }
    }

}
Cat class is extended from  Animal, and implements abstract properties declared in  Animal. 
Cat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPropertyTutorial
{
  
    class Cat : Animal
    {
        private string name;
        private int age;

        // Implement the abstract property declared in the Animal class.
        public override string Name
        {
            get
            {
                return this.name;
            }
        }

        // Implement the abstract property declared in the Animal class.
        public override int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                this.age = value;
            }
        }

        // Constructor.
        public Cat(string name, int age)
        {
            this.name = name;
            this.age = age;
        }
    }

}

Property in Interface

You can also declare a Property in Interface, these properties will be implemented in the subclass.
IColor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPropertyTutorial
{
    interface IColor
    {

        // An abstract property of Interface.
        String Color
        {
            get;
        }
    }

}
Ball Class implement IColor Interface, it implements abstract properties declared in IColor.
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPropertyTutorial
{
   class Ball : IColor
   {
       private string color;
 
       public string Color
       {
           get
           {
               return this.color;
           }
       }

       // Constructor.
       public Ball(String color)
       {
           this.color = color;
       }
   }

}