Properties in C#

Properties

The property member of class provides access to variables. Some examples of properties are font type, color and visible properties. Basically properties are fields. A field member can be accessed directly, but a property member is always accessed through accessor and modifier method called get and set respectively.

Why use Properties if you already have the field available ?

First of all, properties expose fields in classes being used in components. They also provides means for doing necessary computation before or after accessing or modifying the private fields they are representing. For example, if you are changing the Salary of Employee in set method, you may also validate this salary for negative values.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        private bool bGender;

        private int intAge;

        // Gender property.

        public bool MaleGender

        {

            get

            {

                return bGender;

            }

            set

            {

                bGender = value;

            }

        }

        // Age property

        public int Age

        {

            get

            {

                return intAge;

            }

            set

            {

                intAge = value;

            }

        }

        static void Main(string[] args)

        {

            Program prg = new Program();

            prg.MaleGender = true;

            prg.Age = 25;

            if (prg.MaleGender)

                Console.WriteLine("The Gender is Male");

            else

                Console.WriteLine("The Gender is Female");

            Console.WriteLine("Age is" + prg.Age.ToString());

            Console.ReadKey();

        }

    }

}

 
Output



Read only Property

Properties can be made read only. This is accomplished by having only a get accessor in property implementation.

Example
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        private bool bGender = true;

        private int intAge = 45;

        // Gender property.

        public bool MaleGender

        {

            get

            {

                return bGender;

            }

        }

        // Age property

        public int Age

        {

            get

            {

                return intAge;

            }

        }

        static void Main(string[] args)

        {

            Program prg = new Program();

            if (prg.MaleGender)

                Console.WriteLine("The Gender is Male");

            else

                Console.WriteLine("The Gender is Female");

            Console.WriteLine("Age is" + prg.Age.ToString());

            Console.ReadKey();

        }

    }

}

 
Output


Write only Properties

You can assign values to, but not read from, a write only property. A write only property only has only a set accessor.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        private bool bGender;

        private int intAge;

        // Gender property.

        public bool MaleGender

        {

            set

            {

                bGender = value;

            }

        }

        // Age property

        public int Age

        {

            set

            {

                intAge = value;

            }

        }

        private void Show()

        {

            if (bGender)

                Console.WriteLine("The Gender is Male");

            else

                Console.WriteLine("The Gender is Female");

            Console.WriteLine("Age is" + intAge);

        }

        static void Main(string[] args)

        {

            Program prg = new Program();

            prg.MaleGender = false;

            prg.Age = 23;

            prg.Show();

            Console.ReadKey();

        }

    }

}


Output