Thursday 2 February 2012

Encapsulation in C#:


Encapsulation in C#:


Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.

   Encapsulation is process of keeping data and methods together inside objects. In this way developer must define somemethods of object's interaction. In C# , encapsulation is realized through the classes. A Class can contain data structures and methods. Consider the following class.

public class Aperture
{

public Aperture()
{

}

protected double height;
protected double width;
protected double thickness;

public double GetVolume()
{

double volume = height*width*thickness;
if(volume<0)
return 0;
return volume;
}
}

   In this example we encapsulate some data such as height, width, thickness and method GetVolume. Other methods or objects can interact with this object through methods that have public access modifier. It must be done using . operator.

No comments:

Post a Comment