C# Properties

Definition

To properly encapsulate the field c# 3.0 introduces the properties concept.

  1. Read/Write properties
  2. Read-only properties
  3. Write only properties
  4. Auto implement properties

Example of Read/Write properties

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

namespace GetSetProperties
{
    class Student
    {
        private int _passmarks;

        public int Passmarks  //Properties
        {
            get { return _passmarks; }   //get accessors return the value of _passmarks which is set by set accessors
            set
            {
                if (value > 35)   //value inbuilt keyword supplied through user
                {
                    _passmarks = value;
                }
                else
                {
                    Console.WriteLine("Passmark should be greater than 35");
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student std = new Student();
            std.Passmarks = 34; 
            Console.ReadLine();
        }
    }
}

Output of Read/Write properties

Passmark should be greater than 35

Example of Read only properties

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

namespace GetSetProperties
{
    class Student
    {
        private int _passmarks=34;

        public int Passmarks  //Properties
        {
            get { return _passmarks; }   //get accessors return the value of _passmarks which value is 34
         }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student std = new Student();
            Console.WriteLine(std.Passmarks);
            Console.ReadLine();
        }
    }
}

Output of Read only properties

34

Example of Write only properties

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

namespace GetSetProperties
{
    class Student
    {
        private int _passmarks;

        public int Passmarks  //Properties
        {
            set {
                    _passmarks=value;
                 
                }   //we cannot get the value by the properties because it is for write purpose
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student std = new Student();
            std.Passmarks=34;
            Console.ReadLine();
        }
    }
}

If we trying to print _passmarks using get accessor then compiler gives an error

Example of Auto implement properties

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

namespace GetSetProperties
{
    class Student
    {
        private int _passmarks=34;

        public int Passmarks{get;set;}  //Auto implement properties
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student std = new Student();
            std.Passmarks=34;
            Console.WriteLine(std.Passmarks);
            Console.ReadLine();
        }
    }
}

Output of Auto implement properties

34

Note: class may have 100-200 fields in real time projects. when field has no need such as logic like _passmarks greater than 35 then we use the auto implement properties. The compiler automatically creates private field against the property fields.



Write a comment


Name Email Message