Why C# Programmers Use Properties Technique in C# programming?

Asked 25-Aug-2021
Viewed 456 times

0

Why C# Programmers Use Properties Technique in C# programming?


1 Answer


0

Property is a member of a class or object that represents a data item of a class or object. By using property, we read the data of a field or write new data in the field, whereas the syntax of reading or writing the data of the field through the property is the same. Also, we can apply various types of Field Level Business Rules in this property very easily, for which we do not have to write Extra Codes for Business Rules in any method. Like any field, the property also has the following characteristics.
  1. Property is a named member of a class.
  2. Every property has a type.
  3. Property can be read/write i.e. Get/Set.
Memory is not allocated to the property to store the data.
The property of a class is a named set of pair of matching methods called accessors as follows:
  1. set Accessors are used to assign or write a value to a property.
  2. get Accessors are used to retrieve or read the value of a property.
Syntax
class class-name
{
 [ access-modifier ] datatype variable-name / property-name
 {
  set;	// auto complete setter property
  get; 	// auto complete getter property
 }
. . .
}
class class-name
{
 [ access-modifier ] datatype variable-name / property-name
 {
  set	// setter property
  {
   SetAccessorCode
  }
  get 	// getter property
  {
   GetAccessorCode
  }
 }
. . .
}
Property Declaration and Accessors
Syntax and Semantic of getting and set Accessors are Predefined. Therefore, we can consider a set accessor as a method that assigns or sets a value to a single parameter. Whereas get Accessor does not accept any parameter but returns the value of a specific property.
The set Accessor always has a single, Implicit Value Parameter named value, which has the same type as the property and its return type is void. Whereas there is no parameter in getting Accessor and its return type is the same, which is the type of property.
Read-Only and Write-Only Properties
We can keep either of the gets or set Accessor undefined, but both cannot be kept undefined. The property in which only get Accessor is defined, that property is called Read-Only Property. Whereas the property in which only set accessor is defined is called Write-Only Property.
Automatically setter and getter property
Which provides us the facility to declare properties directly without declaring Backing Fields. Because the C# compiler itself creates a Hidden Backing Field for each of our properties and hooks the get or set Accessor with it.
When we define Automatically Implemented Properties, C# Compiler automatically creates Backing Field according to the type of value of our property and allocates memory to them.
When we want to get Automatically Implemented Properties, then we do not have to define get and set accessors, rather we have to specify a semicolon immediately after get and set.
The main advantage of the property
 All C# programmers use property because the property has no take memory space as compare to function. All instance functions take space in memory. After using property, our program is faster than a function-oriented class.

Example
using System;
class Circle
{
 private const float PI=3.14159f; // constant variable
 private int radis;		// instance variable
 public int radious		// properties of class
 {
  set{			// setter property assign value in radis variable
   radis=value;
  }
  get{			// getter property return value of radis variable
   return radis;
  }
 }
 public float Area(){		// member function 
  return radious > 0 ? (radious*radious*PI) : 0.0f;
 }
}
public class Program
{
 public static void Main()
 {
  Circle cir=new Circle();
  cir.radious = 5;		// setter property call 
  Console.WriteLine('Area : '+cir.Area());
 }
}
Output
Area : 78.53975