Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Encapsulation

C# Encapsulation

Encapsulation Defined as "enclosing one or more items in a physical or logical package." In object-oriented programming methodology, encapsulation is intended to prevent access to implementation details.

Abstraction and encapsulation are related features of object-oriented programming. Abstraction allows relevant information to be made visible, while encapsulation enables developers toachieve the required level of abstraction。

C# encapsulation sets access permissions for users according to specific needs, and through access modifiers to do so.

A access modifiers defines the scope and visibility of a class member. The access modifiers supported by C# are as follows:

Public Access Modifier

The Public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed by external classes.

The following example illustrates this:

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

(

)

)

;

       

}

   

}

// End of Rectangle

   

class

ExecuteRectangle

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

            Rectangle r

=

new

Rectangle

(

)

;

            r

.

length

=

4.5

;

            r

.

width

=

3.5

;

            r

.

Display

(

)

;

            Console

.

ReadLine

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



长度: 4.5

宽度: 3.5

面积: 15.75

In the above example, the member variables length and width are declared as public, so they can be used by the function Main() through an instance of the Rectangle class r .

member function Display() and GetArea() and these variables can be accessed directly.

member function Display() is also declared as public, so it can also be Main() accessed through an instance of the Rectangle class r .

Private Access Modifier

The Private access modifier allows a class to hide its member variables and member functions from other functions and objects. Only functions within the same class can access its private members. Even an instance of the class cannot access its private members.

The following example illustrates this:

Example

using

System

;

namespace

RectangleApplication

{

   

class

Rectangle

   

{

       

// private member variables

       

private

double

length

;

       

private

double

width

;

       

// public method to get the rectangle's length and width from user input

       

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 method to calculate the rectangle's area

       

public

double

GetArea

(

)

       

{

           

return

length

width

;

       

}

       

// public method to display the rectangle's properties and area

       

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

)

       

{

           

// Create an instance of the Rectangle class

            Rectangle r

=

new

Rectangle

(

)

;

           

// Get the rectangle's length and width from user input via the public method AcceptDetails()

            r

.

AcceptDetails

(

)

;

           

// Display the rectangle's properties and area via the public method Display()

            r

.

Display

(

)

;

            Console

.

ReadLine

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



Please enter the length:

5

Please enter the width:

3

长度: 5

宽度: 3

面积: 15

Explanation:

Protected Access Modifier

The Protected access modifier allows subclasses to access the member variables and member functions of their base class. This helps achieve inheritance. We will discuss this in detail in the chapter on inheritance. Discussed in more detail here.

Internal Access Modifier

The Internal access modifier allows a class to expose its member variables and member functions to other functions and objects in the current program. In other words, any member with the internal access modifier can be accessed by any class or method within the application in which the member is defined.

The following example illustrates this:

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

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



长度: 4.5

宽度: 3.5

面积: 15.75

In the above example, note that the member function GetArea() is declared without any access modifier. If no access modifier is specified, the default access modifier for a class member is used, which is private。

Protected Internal Access Modifier

The Protected Internal access modifier allows access within this class, derived classes, or the assembly containing this class. It is also used to achieve inheritance.