Encapsulation in C#

Walden Systems Geeks Corner Encapsulation  programming how to tutorial Rutherford NJ New Jersey NYC New York North Bergen County
C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.

Encapsulation, in object oriented programming, prevents access to implementation details. Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction. In C#, encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# uses the following access modifiers: public, private, protected, internal and protected internal. If a method or variable does not have an access modifier, it is given the private modifier by default.

Public

Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class. In the example below, the variables height and width are declared public so they can be accessed from an instance of the class Triangle. The methods GetArea and Display are also accessible from the instance of the class Triangle.

using System;

namespace TriangleApp 
{
   class Triangle 
   {
      public double height;
      public double width;
      
      public double GetArea() 
      {
         return length * width / 2 ;
      }
      public void Display() 
      {
         Console.WriteLine("Height: {0}", height);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }
   
   class ExecTriangle 
   {
      static void Main(string[] args) 
      {
         Triangle myShape = new Triangle();
         myShape.length = 4.0;
         myShape.width = 3.0;
         myShape.Display();
         Console.ReadLine();
      }
   }
}


Private

Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members. The example below is the same as the previous example except in this case, since the method GetArea is declared private, it is not available outside the class.

namespace TriangleApp 
{
   class Triangle 
   {
      private double height;
      private double width;
      
      private double GetArea() 
      {
         return length * width / 2.0 ;
      }
      public setDimensions( double p_height, double p_width )
      {
         width = p_width ;
         height = p_height ;
      }
      public void Display() 
      {
         Console.WriteLine("Height: {0}", height);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }
   
   class ExecTriangle 
   {
      static void Main(string[] args) 
      {
         Triangle myShape = new Triangle();
         myShape.setDimensions( 3.0, 4.0 )
         myShape.Display();
         Console.ReadLine();
      }
   }
}

Protected

Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.


Internal


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

namespace TriangleApp 
{
   class Triangle 
   {
      internal double height;
      internal double width;
      
      double GetArea() 
      {
         return length * width / 2 ;
      }
      public void Display() 
      {
         Console.WriteLine("Height: {0}", height);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }
   
   class ExecTriangle 
   {
      static void Main(string[] args) 
      {
         Triangle myShape = new Triangle();
         myShape.length = 4;
         myShape.width = 3;
         myShape.Display();
      }
   }
}

Protected Internal

The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance. It allows inherited types, even though they belong to a different assembly, they have access to the protected internal members. Types that reside in the same assembly also have access to the protected internal members, even if they are not derived from the type.