codescracker


c#

C# Encapsulation



« Previous Tutorial Next Tutorial »


Encapsulation in C#, is a process of enclosing one or more item(s) within a physical or logical package. Encapsulation prevents access to the implementation details.

Encapsulation is implemented simply by using the access modifiers. An access modifier defines the scope and the visibility of class member. Here are the access modifiers listed, available in C#:

C# Public Access Specifier

In C#, public access specifier allows a class to expose its member variables and members functions to the other functions and objects. Public members can be access from outside the class. Here is an example demonstrating public access specifier in C#:

/* C# Encapsulation - Example Program */
		
using System;
namespace EncapsulationExample
{
	class EncapsulationClass
	{
		// here are member variables
		public double len;
		public double wid;	
		public double CalculateArea()
		{
			return len * wid;
		}	
		public void PrintValue()
		{
			Console.WriteLine("Length = {0}", len);
			Console.WriteLine("Width = {0}", wid);
			Console.WriteLine("Area = {0}", CalculateArea());
		}
	} // end of the class EncapsulationClass
	
	class ExecuteClass
	{
		static void Main(string[] args)
		{
			EncapsulationClass c = new EncapsulationClass();
			c.len = 4.5;
			c.wid = 3.5;
			c.PrintValue();
			Console.ReadLine();
		}	
	}
}

When the above code is compile and executed, it will produce the following output:

Length = 4.5
Width = 3.5
Area = 15.75

As you can see from the above C# program, there are member variables, namely len and wid, which are declared as public, it means they can be accessed from outside the class where they are declared. Therefore these variables are accessed from the Main() function using an instance of the EncapsulationClass class, named c.

And, the member functions namely PrintValue() and the CalculateArea() can also access these variables directly without using any instance of the class. The member function PrintValue() is also declared as public, so this member function can also be accessed from outside, that is from the Main() function using an instance of the EncapsulationClass class, named c. Now let's discuss about the private access specifier in C#.

C# Private Access Specifier

Private access specifier is used to hide its member variables and member functions from outside the class where they are declared, or from the other functions and objects which are not in the class where the private member variables and member functions are declared. Here is an example program, demonstrating the private access specifier in C#:

/* C# Encapsulation - Example Program */
		
using System;
namespace EncapsulationExample 
{
	class EncapsulationClass
	{
		private double len;
		private double wid;
		
		public void GetValues()
		{
			Console.WriteLine("Enter Length Value: ");
			len = Convert.ToDouble(Console.ReadLine());
			Console.WriteLine("Enter Width Value: ");
			wid = Convert.ToDouble(Console.ReadLine());
		}	
		public double CalculateArea()
		{
			return len * wid;
		}	
		public void PrintValue()
		{
			Console.WriteLine("Length = {0}", len);
			Console.WriteLine("Width = {0}", wid);
			Console.WriteLine("Area = {0}", CalculateArea());
		}	
	}
   
	class ExecuteClass
	{
		static void Main(string[] args)
		{
			EncapsulationClass c = new EncapsulationClass();
			c.GetValues();
			c.PrintValue();
			Console.ReadLine();
		}	
	}
}

When we compile and run the above code, it will produce the following output:

Enter Length Value:
4.4
Enter Width Value:
3.3
Length = 4.4
Width = 3.3
Area = 14.52

Here, as you can see from the above C# program that the member variables namely len and wid, are declared as private. Therefore they can't be accessed from the Main() function. And the member functions namely GetValues and the PrintValue() can access these variables, because theese members functions are declared as public, so they can be accessed from the Main() function using an instance of the EncapsulationClass class, named c

C# Protected Access Specifier

Protected access specifier in C#, allows a child class to access the member variables and the member functions of its base class.

C# Internal Access Specifier

Internal access specifier in C#, allows a class to expose its member variables and the members functions to the other functions and objects in the current assembly. Here is an example, demonstrating the concept of internal access specifier in C#:

/* C# Encapsulation - Example Program */
		
using System;
namespace EncapsulationExample
{
	class EncapsulationClass
	{
		internal double len;
		internal double wid;
		
		double CalculateArea()
		{
			return len * wid;
		}	
		public void PrintValue()
		{
			Console.WriteLine("Length = {0}", len);
			Console.WriteLine("Width = {0}", wid);
			Console.WriteLine("Area = {0}", CalculateArea());
		}	
	}
	
	class ExecuteClass
	{
		static void Main(string[] args)
		{
			EncapsulationClass c = new EncapsulationClass();
			c.len = 4.5;
			c.wid = 3.5;
			c.PrintValue();
			Console.ReadLine();
		}
	}
}

When we compile and run the above code, it will produce the following output:

Length = 4.5
Width = 3.5
Area = 15.75

C# Protected Internal Access Specifier

Protected internal access specifier in C#, is used to hide its member variables and the member functions from the other class objects and functions, except a child class within the same application.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test