KamranCSharp - whereIstand.com

Thursday, December 4, 2008

Chapter 3: Encapsulation


Encapsulation
The notion of encapsulation programming logic is the idea of data hiding.
Using accessor and mutator methods we can make encapsulation, or by using properties.
ENCAPSULATION USING PROPERTIES:
Properties are a new feature introduced with C#. Properties in C# helps to protect a field in a class by reading and writing to it. Encapsulation can be accomplished much better with properties. Now let's see an example:

public class Department
{
private string departname;
public string Departname
{
get
{
return departname;
}
set
{
departname=value;
}
}
}
public class Departmentmain
{
public static int Main(string[] args)
{
Department d= new Department();
d.departname="Speaking";
Console.WriteLine("The Department is :{0}",d.Departname);
return 0;
}
}