SoFunction
Updated on 2025-03-06

In-depth analysis of c# encapsulation and access modifiers

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

Abstraction and encapsulation are related features of object-oriented programming. Abstraction allows visualization of relevant information, while encapsulation allows developers to achieve the required level of abstraction.

C# encapsulation sets user access rights according to specific needs and implements 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 the object of this class and its subclass objects can be accessed
  • internal: Objects of the same assembly can be accessed;
  • protected internal: Access to types limited to the current assembly or derived from the contained class.

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:

using System;

namespace RectangleApplication
{
  class Rectangle
  {
    //Member variable    public double length;
    public double width;

    public double GetArea()
    {
      return length * width;
    }
    public void Display()
    {
      ("length: {0}", length);
      ("width: {0}", width);
      ("area: {0}", GetArea());
    }
  }// Rectangle ends
  class ExecuteRectangle
  {
    static void Main(string[] args)
    {
      Rectangle r = new Rectangle();
       = 4.5;
       = 3.5;
      ();
      ();
    }
  }
}

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

Length: 4.5
Width: 3.5
Area: 15.75

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

Member functions Display() and GetArea() can directly access these variables.

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

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 in the same class can access its private members. Even instances of a class cannot access its private members.

The following example illustrates this:

using System;

namespace RectangleApplication
{
  class Rectangle
  {
    //Member variable    private double length;
    private double width;

    public void Acceptdetails()
    {
      ("Please enter the length:");
      length = (());
      ("Please enter the width:");
      width = (());
    }
    public double GetArea()
    {
      return length * width;
    }
    public void Display()
    {
      ("length: {0}", length);
      ("width: {0}", width);
      ("area: {0}", GetArea());
    }
  }//end class Rectangle  
  class ExecuteRectangle
  {
    static void Main(string[] args)
    {
      Rectangle r = new Rectangle();
      ();
      ();
      ();
    }
  }
}

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

Please enter the length:
4.4
Please enter the width:
3.3
Length: 4.4
Width: 3.3
Area: 14.52

In the above example, the member variables length and width are declared private, so they cannot be accessed by the function Main() .

Member functions AcceptDetails() and Display() can access these variables.

Since member functions AcceptDetails() and Display() are declared public, they can be accessed by Main() using instance r of the Rectangle class.

Protected Access Modifier

The Protected access modifier allows a subclass to access its base class's member variables and member functions. This helps achieve inheritance. We will discuss this in detail in the inheritance chapter. Discuss this in more detail.

Internal access modifier

The Internal access specifier 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 defined within the application defined by that member.

The following example illustrates this:

using System;

namespace RectangleApplication
{
  class Rectangle
  {
    //Member variable    internal double length;
    internal double width;
    
    double GetArea()
    {
      return length * width;
    }
    public void Display()
    {
      ("length: {0}", length);
      ("width: {0}", width);
      ("area: {0}", GetArea());
    }
  }//end class Rectangle  
  class ExecuteRectangle
  {
    static void Main(string[] args)
    {
      Rectangle r = new Rectangle();
       = 4.5;
       = 3.5;
      ();
      ();
    }
  }
}

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

Length: 4.5
Width: 3.5
Area: 15.75

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

Protected Internal Access Modifier

The Protected Internal access modifier allows access in this class, derived classes, or assemblies containing the class. This is also used to implement inheritance.

The above is the detailed content of in-depth analysis of the c# encapsulation and access modifiers. For more information about c# encapsulation and access modifiers, please follow my other related articles!