Encapsulation in C#

Now we are going to learn about the Encapsulation In C#.
  • 4244

Introduction

Encapsulation is the process of data binding. It means that encapsulating the data from outside access. In object oriented programming encapsulation play a very important role because encapsulation provides protection of data from accident corruptions.

Example

class Rectangle
{
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
 {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console
.WriteLine("Area: {0}", GetArea());
}

class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 6.5;
r.width = 5.5;
r.Display();
Console.ReadLine();
}
}
}

Output

Encapsulation.jpg

© 2013 dotNetheaven. All rights reserved.