Monday, 5 August 2013

Get accessor

Get accessor
The execution of the
get
accessor is equivalent to reading the value of the field.
The following is a
get
accessor that returns the value of a private field name:
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
}
Set accessor
The
set
accessor is similar to a method that returns
void
. It uses an implicit parameter
called
value
, whose type is the type of the property. In the following example, a
set
accessor is added to the Name property:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
When you assign a value to the property, the
set
accessor is invoked with an argument
that provides the new value. For example:
e1.Name = "Reshmi"; // The set accessor is invoked here
It is an error to use the implicit parameter name (
value
) for a local variable declaration in
a
set
accessor.

No comments:

Post a Comment