Concept of Property with C#

Introduction

Property In C#


A property is an entity that describes the features of an object. A property is a piece of data contained within a class that has an exposed interface for reading/writing.

Main purpose

Properties are used to access our private class members and used them like a variables.
Properties are special kind of method or block which have two successors and this property cant take any arguments.
Properties are look like a variables.
Properties are used to security access our private class members.
Properties increase usability with maintaining security.

The two successor of Properties are "get" and "set"
get --
used to return the value.
set -- used to assign the value.

Syntax of Property

public returnType  PropertyName
{
  set   { privateVariable = value;}
  get   { return privateVariable;  }
}

Types of Properties

1) Read-Only (only get)
2) Write-Only(only set)
3) Normal    (get & set both)

Program Read-Only

using System;
namespace property
{
    class ReadOnlyProp
    {
        string name="DEEPAK DWIJ "; // private variable-bcoz ByDefault member variable of class is Private.
         public string ROnly  //  Read only property only returns the private variable.
        {
            get { return name; }
        }
    }
    class Program:ReadOnlyProp
    {
        public static void Main()
        {
            Program rdonly = new Program();
            Console.WriteLine(rdonly.ROnly);
            Console.Read();
        }
   }
}

Output

readonly.gif

Program WriteOnly

using System;
namespace WriteOnly
{   
class
UseSet
   {
       private string name = string.Empty;
        public string Wonly // ONLY SET PROPETY
        {
            set { name = value; }
        }
        public void DisplayData()
        {
            Console.WriteLine("Name: {0}",name);
        } 
    }
    class Program : UseSet
    {
        public static void Main(string[] args)
        {
            Program wrt = new Program();
            wrt.Wonly = "DEEPAK SHARMA";
            wrt.DisplayData();
            Console.Read();
        }
     }
}

Output
 

write-only.gif

Program Normal(set and get both)

using System;
namespace Deep
{
   abstract class Car
    {
       string regno;
       int modelno;
       public int Model // This is property 1st
       {
           set { modelno = value; }
           get { return modelno; }
       }
       public string Resgistration // This is property 2nd
       {
           set { regno = value; }
           get { return regno; }
       }
       public void brakes() // function
       {
           Console.WriteLine("Brakes are important to drive safely");
       }    
    }
  class Maruti:Car
   {  
     public static void Main()
       {
           Maruti mr = new Maruti();
           Console.WriteLine("        Maruti CAR DESCRIPTION");
           Console.WriteLine("*******************************************");
           mr.Model = 2005;
           mr.Resgistration = "UP-14 Z 6224-DEEPAK";
           Console.WriteLine("CarModel        : " + mr.Model + "");
           Console.WriteLine("CarRegistration : " + mr.Resgistration + "");
           mr.brakes();
           Console.WriteLine("*******************************************");
           Console.Read();
       }
  }
}

Output

prperty.gif