Encapsulation is defined as “enclosing one or more projects in a physical or logical package”. In object-oriented programming methodology, encapsulation is designed to prevent access to implementation details.
Abstraction and encapsulation are related features of object-oriented programming. Abstraction allows visualization of related information, while encapsulation enables developers to achieve the required level of abstraction.
According to the specific needs, C# encapsulation sets the access rights of users, and realizes it through access modifiers.
An access modifier defines the scope and visibility of a class member. The access modifiers supported by C# are as follows:
Public: all objects are accessible;
Private: the object itself can be accessed inside the object;
Protected: only this class object and its subclass objects can access
Internal: Objects in the same assembly can be accessed;
Protected internal: access is limited to the current assembly or a type derived from the containing class.
The following example illustrates this: When the above code is compiled and executed, it produces the following results: In the above example, the member variable Member function Member function The following example illustrates this: When the above code is compiled and executed, it produces the following results: In the above example, the member variable Member function Because of the member function The Protected access modifier allows a subclass to access member variables and member functions of its base class. This helps to implement inheritance.We will discuss this in detail in the chapter on inheritance. Discuss this in more detail. The following example illustrates this: When the above code is compiled and executed, it produces the following results: In the example above, notice the member function 
1.22.1. Public access modifier #
Public
access modifiers allow a class to expose its member variables andmember functions to other functions and objects. Any public member can beaccessed by an external class.Example #
using System;
namespace RectangleApplication
{
class Rectangle
{
//Member variables
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());
}
}// Rectangle end
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
Length: 4.5
Width: 3.5
Area: 15.75
length
and
width
are declared as
public
, so they cannot be used by functions
Main()
accessing instances of the
Rectangle
class using
r
.
Display()
and
GetArea()
, you can access these variables directly.
Display()
is also declared to be
public
, so it canalso be accessed by
r
instances of the
Rectangle
class using
Main()
. 1.22.2. Private access modifier #
Private
access modifiers allow a class to hide its member variables and member functions from other functions and objects. Only functions in the same class can access its private members. Even an instance of a class cannot access its private members.Example #
using System;
namespace RectangleApplication
{
class Rectangle
{
//Member variables
private double length;
private double width;
public void Acceptdetails()
{
Console.WriteLine("Please enter the length:");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter the width:");
width = Convert.ToDouble(Console.ReadLine());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("length: {0}", length);
Console.WriteLine("width: {0}", width);
Console.WriteLine("area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
Please enter the length:
four point four
Please enter the width:
three point three
Length: 4.4
Width: 3.3
Area: 14.52
length
and
width
are declared as
private
, so they cannot be used by functions
Main()
visit.
AcceptDetails()
and
Display()
, which you can access thesevariables.
AcceptDetails()
and
Display()
are declared as
public
, so they can be accessed by
r
instances of the
Rectangle
class using
Main()
. 1.22.3. Protected access modifier #
1.22.4. Internal access modifier #
Internal
access specifiers allow a class to expose its member variables and member functions to other functions and objects in the current program. In other words, with
internal
any member of the access modifier can be accessed by any class or method within the application defined by that member.Example #
using System;
namespace RectangleApplication
{
class Rectangle
{
//Member variables
internal double length;
internal double width;
double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("length: {0}", length);
Console.WriteLine("width: {0}", width);
Console.WriteLine("area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
Length: 4.5
Width: 3.5
Area: 15.75
GetArea()
declared without any access modifiers. If no access modifier is specified, the default access modifier for class members is used, that is,
private
. 1.22.5. Protected Internal access modifier #
Protected
Internal
access modifiers are allowed to be accessed in this class, derived classes, or assemblies that contain the class. This is also used to implement inheritance.