Per your comment, you want to execute a code before you are setting the value. So simple example would be:
class Example
{
DayOfWeek _day;
public DayOfWeek Day
{
// whenever you try to get this value, it will run this block
get
{
// We don't allow this to be used on Friday.
if (this._day == DayOfWeek.Friday)
{
throw new Exception("Invalid access");
}
return this._day;
}
// whenever you try to set its value, it will run this block
set
{
// We dont allow to set if it is saturday
if (value == DayOfWeek.Saturday)
{
throw new Exception("Invalid access");
}
this._day = value;
}
}
}
Getters and setters basically let you execute "lightweight" code before you access/set the value.
BUT if you are executing code in a property, make sure you've written a property and not a method. A property should do less work-- a lot less work-- than a method. Properties should be lightweight. If your property incurs significant effort, it should be refactored into an explicit method.
There are more to that.. Please read this post as there are some helpful content.