- Property is a named member of a class.
- Every property has a type.
- 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:
- set Accessors are used to assign or write a value to a property.
- 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