Properties are used to protect the field in a class by reading and writing it through the property. Here the field consists of one getter and one setter that are associated with a given field. We can access this field through this getter/setter combination.
Two ways to declare property in the class
- One way to declare:
class PropertyDemo {
private String name; //Declare a field
public String getName() { //Getter
return name;
}
public void setName( String Value) {//Setter
name = value;
}
}
Another way to declare:
class PropertyDemo {
private String name; //Declare a field
public String Name{
get{ return name;} //Getter
set{ name = value;} // Setter
}
}
Read-Only property:
We can declare a property as read-only; by only have its getter. So that the user can't set the value of the field, because there is no setter for the field. Hence we restrict the user to change the value of the field at any given time.
Write-Only property:
We can declare a property as write-only, by only have its setter. Here the user can't access the value that is there in the field. We can manipulate the value of the field by using its setter, but we can't get the value of this field.
Example: Demonstarte propery, Read-Only Property, Write Only Property.

ClsProperty9.cs
using System;
usingusing System.Collections.Generic;
using System.Text;
namespace cSHARPEXAMPLES
{
class ClsProperty9
{
private int age;
private String name;
private String lastName = "Dogra" ;
private String address;
public int getAge() {
return age;
}
public void setAge( int Value) {
age = Value;
}
public String getName()
{
return name;
}
public void setName( String Value)
{
name = Value;
}
//Read-Only property:Only Getter
public String getLastName()
{
return lastName;
}
//Write-Only Property: Only Setter
public void setAddress( String Value)
{
address = Value;
}
public String displayAddress()
{
return address;
}
}
}
Form9.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace cSHARPEXAMPLES
{
public partial class Form9 : Form
{
ClsProperty9 obj = new ClsProperty9 ();
public Form9()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{ //Setters Calls
obj.setAge( Int16 .Parse(textBox1.Text));
obj.setName(textBox2.Text);
obj.setAddress( " Chandigarh " ); //Write Only Property
}
private void button2_Click( object sender, EventArgs e)
{ //Getters Calls
MessageBox .Show( "Name: " + obj.getName());
MessageBox .Show( "Age: " + obj.getAge());
MessageBox .Show( "Last Name: " + obj.getLastName()); //Read
//Only property
MessageBox .Show( "Address: " + obj.displayAddress());
}
}
}
Output:

Click On: Set the Value
This will set the values in the class fields i.e. name, address
Click On: Get the Value
This will display the values that are set by the setters.
First value that we get is the name.
Second is the age value.
This field only has getter associated to it.
This field only has setter associated with it, we can





