Object oriented programming
languages support encapsulation, which is also called as data hiding. Encapsulation
is hiding the private variables of a class from the external world, if we are
hiding the members then how do we get and set the values of these members? It
is when Properties become significant.
Properties define get and set assessors which can be used to get and set values of private variables in a class.
The get {} property returns the values of the underlying variable to the
external world.Properties define get and set assessors which can be used to get and set values of private variables in a class.
The set{} property receives the values from outside and sets it to the underlying variable.
The following example defines a simple class which contains a private variable strName and a set of get{} and set{} properties to expose the variable to the external world.
class Employee
{
private string
strName;
//
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}
}
No comments:
Post a Comment