Coding With Fun
Home Docker Django Node.js Articles FAQ

The C#package


May 11, 2021 C#


Table of contents


The C#encapsulation

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

Abstraction and encapsulation are related characteristics of object-oriented program design. A bstraction allows for visualization of relevant information, while encapsulation enables programmers to implement the required level of abstraction.

Encapsulation is implemented using the access modifier. A n access modifier defines the scope and visibility of a class member. The access modifiers supported by C# are as follows:

  • Public
  • Private
  • Protected
  • Internal
  • Protected internal

Public access modifiers

Public access modifiers allow a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed by an external class.

The following example illustrates this point:

using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        public double length;
        public double width;

        public double GetArea()
        {
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {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 above code is compiled and executed, it produces the following results:

长度: 4.5
宽度: 3.5
面积: 15.75

In the example above, 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 also access these variables directly from instances of the class.

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

Private access modifiers

Private access modifiers allow a class to hide its member variables and member functions from other functions and objects. O nly functions in the same class can access its private members. Even an instance of a class cannot access its private members.

The following example illustrates this point:

using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        private double length;
        private double width;

        public void Acceptdetails()
        {
            Console.WriteLine("请输入长度:");
            length = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("请输入宽度:");
            width = Convert.ToDouble(Console.ReadLine());
        }
        public double GetArea()
        {
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {0}", GetArea());
        }
    }//end class Rectangle    
    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.Acceptdetails();
            r.Display();
            Console.ReadLine();
        }
    }
}

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

请输入长度:
4.4
请输入宽度:
3.3
长度: 4.4
宽度: 3.3
面积: 14.52

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

The member functions AcceptDetails() and Display() can access these variables.

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

Protected access modifiers

The Protected access modifier allows sub-classes to access member variables and member functions of its base class. T his helps with inheritance. W e'll discuss this in more detail in the inherited section. Discuss this in more detail.

Internal access modifiers

The Internal access descriptor 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 an internal access modifier can be accessed by any class or method within the application defined by that member.

The following example illustrates this point:

using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        internal double length;
        internal double width;
        
        double GetArea()
        {
            return length * width;
        }
       public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {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 above code is compiled and executed, it produces the following results:

长度: 4.5
宽度: 3.5
面积: 15.75

In the example above, 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 private.

Protected Internal access modifiers

The Protected Internal access modifier allows a class to hide its member variables and member functions from other class objects and functions than sub-classes within the same application. This is also used to implement inheritance.

Range comparison

  • (1) Pubilc: Any public member can be accessed by an external class.
  • (2) Private: Only functions in the same class can access its private members.
  • (3) Protected: Accessible within and within the class.
  • (4) internal: Objects of the same assembly are accessible.
  • (5) The combination of Protected internal: 3 and 4, accessible in accordance with any one.

That is:

private < internal/protected < protected internal < public