o7planning

C# Properties Tutorial with Examples

  1. C# Property
  2. Abstract Property

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 externally, 
        // this field must be 'public' or 'protected'.
        public string code;

        // To be accessible externally, 
        // 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 an Employee object.
            Employee john = new Employee("E01", "John");

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

            // But 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 Propertyto 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,
        // It does not allow external access.
        private string code;

        // This field is private,
        // It does not allow external access.
        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 property.......
                this.code = value;
            }
        }

        // Declare a property, public, allows access,
        // but not allowed to assign new values.
        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 an Employee object.
            Employee2 john = new Employee2("E01", "John");

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

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

            // Can not assign new value to Name property.
            // john.Name = "Marry"; // ==> Error!
            Console.WriteLine("Employee Code = " + john.Code);
            Console.WriteLine("Employee Name = " + john.Name);

            Console.Read();
        }
    }

}
Run the example:
Employee Code = E01
Employee Name = John
------
Employee Code = E02
Employee Name = John

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 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; 
        // Implements the declared property in Interacle IColor
        // (Do not write the keyword 'override' here,
        // because it implements an interface property).
        public string Color
        {
            get
            {
                return this.color;
            }
        } 
        // Constructor.
        public Ball(String color)
        {
            this.color = color;
        }
    } 
}