
In simple a property allow you to embed your own logic with the field of the Class.
Here logic mean anything. :)
1. It can be just allow to only
RETRIEVE ONLY or
READ , you
in this case you will implement
only get .
public int ID
{
get;
}
private string _somevalue;
public string SomeValue
{
get { return _somevalue; }
}
2. Similarly it will only allow you to
ASSIGN or
SET the value.
in this case you will implement
only set .
public int ID
{
set;
}
private string _somevalue;
public string SomeValue
{
set { _somevalue = value; }
}
3. Expose a part of the Controls Field.
Lets say in a Form one Label Control is there and you are interested to access/ modify its
Text Property ONLY.
in this you could do like this
public String LabelValue
{
get { return lblForm.Text; }
set { lblForm.Text = value; }
}
4. You can perform some validation also
Lets say some property called ID should not be negative.
public static int ID
{
set
{
if (value < 0)
throw new ArgumentException("Negative values are not allowed");
}
}
Similarly many more....
To know more about property
http://msdn.microsoft.com/en-us/library/w86s7x04.aspx
Thanks,
Narayan
Akiii, if this helps please login to Mark As Answer. | Alert Moderator