In this blog, I am going to talk about Encapsulation and Access Specifiers in C#.
Why we should use encapsulation
Whenever a program or project is developed, data security and privacy is the main concern for a user before using it. From a developer's or programmer's perspective, code misuse or access and modification of the code/data from unnecessary sources or methods call are the main concerns. Encapsulation is one of the best ways to stop this.
Encapsulation
- Encapsulation is one of the most important features of an object-oriented language.
- Encapsulation is a process of isolating the code/data from direct access by implementing access specifiers with it.
- Encapsulation is performed to prevent the code/data from unnecessary modification, from an unauthorized user and protect the data/code from getting corrupt.
- Encapsulation is a process of enclosing the data, functions into a single logical unit called class.
- Encapsulation is a way of hiding the class members from outside classes.
- It is called as Information hiding also.
Access Specifiers
Access Specifiers are used in defining the scope and visibility of a class and its members. Access Specifiers are of the following types,
- Public
- Private
- Protected
- Internal
- Protected Internal
Public
a) 'public' keyword is used to declare a class or class members with public access specifiers.
b) A public class or public class members can be used anywhere in the code. The public class or Public members can be accessed outside their class.
c) Classes and structs declared directly inside namespace can be public or internal. If no access modifier is specified, then they are set to 'internal' by default.
d) Class members or struct members are by default private if no access modifier is specified.
- using System;
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
- calci.num1 = 100;
- calci.num2 = 200;
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- public class Calci
- {
- public double num1;
- public double num2;
- public double result;
- public double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
- public double Multiply(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 * num2;
- return result;
- }
- }
- }
In the program above, two public classes 'Program' and 'Calci' are created. 'Calci' class contains all public members. In the 'Main' method, after creating the object of 'Calci' class, we can access all the public members. You may be wondering what will happen when we change the access specifier of 'Calci' class to private? If we do so, then the compiler will generate an error as "Elements defined in a namespace cannot be explicitly declared as private, protected or protected internal" as shown in the below program.
- using System;
- namespace Tutpoint
- {
- public class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
- calci.num1 = 100;
- calci.num2 = 200;
- calci.Sum(33, 44);
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- // Compiler will generate an error as "Elements defined in a namespace cannot be explicitly declared as private, protected or protected internal"
- private class Calci
- {
- public double num1;
- public double num2;
- public double result;
- public double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
- }
- }
Private
a) 'private' keyword is used to declare a class or class members with private access specifiers.
b) Private members of a class can only be accessed within the class. They cannot be accessed outside the class. In other words, Private members of a class are hidden outside the class.
c) Classes and structs declared directly inside namespace cannot be private. They can only be public or internal.
- using System;
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
- // variable 'num1' of class 'Calci' is inaccessible because it is declared with private and we are trying to access it outside the class
- // Compiler will generate an error as "'calci.num1' is inaccessible due to its protection level"
- calci.num1 = 100;
- calci.num2 = 200;
- // Method 'Sum' of class 'Calci' is inaccessible because it is declared with private and we are trying to access it outside the class
- // Compiler will generate an error as "'calci.Sum(int,int)' is inaccessible due to its protection level"
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- public class Calci
- {
- //Variable with private access specifier
- private double num1;
- //Variables with public access specifier
- public double num2;
- public double result;
- //Method with private access specifier
- private double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
- //Method with public access specifier
- public double Multiply(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 * num2;
- return result;
- }
- }
- }
In the program above, two public classes 'Program' and 'Calci' are created. 'Calci' class contains private and public members. In the 'Main' method, after creating the object of 'Calci' class, we can access only the public members. When we try to access the private members outside the class then compiler will generate an error as "Element is inaccessible due to its protection level".

Gaurav GuptaPosted Jan 11, 2018, 11:47 PM
Nice article keep it..