Encapsulation can be achieved using C# properties. Encapsulation means hiding a complex object from the outer world, in other words not exposing sensitive information to the client.
For which we will require Private members and as that is not accessible to the outer world we initialize the values using Properties.
Basically, Property contains two methods Get and Set.
Example,
class Car
{
private string model; //Field member
public string Model //Property
{
get
{
return model; //Get method
}
set
{
model = value; //Set method
}
}
}
Achieving encapsulation,
- Provides control for the class members
- Making fields read-only (Only get method) and write-only (Only set method)
- Provides data security
No comments:
Post a Comment