Encapsulation in C#

Encapsulation is the process of wrapping up data and members of a class. It is an approach to hidding the irrelevant information and showing only the relevant information of a specific object to a user.
Encapsulation restricts users from sharing and manipulating data , thereby minimizing the chances of data curruption and unauthorized access of data.
Points to Remember

  • Encapsulation provides a way to protect our data from unauthorized access, for this instead of defining our data as public, we can declare those fields (such as data members ,member functions ,properties etc) as Private. Private data can indirectly be operated on two ways , firstly through GET and SET methods and Secondly through the named properties.
  • Encapsulation increases the maintainability of the code by showing only the relevant information to a user.
  • Encapsulation prevents data corruption by enabling private member variables of a class to be accessed through specific methods or properties.
  • Encapsulation binds member variables and functions of a class into a single unit. This is also called wrapping up of data members and member functions in a class
  • Encapsulation is implemented by using the Private access specifier in a class
  • We can implement Encapsulation using GET and SET
  • We can implement Encapsulation using Properties

What is Extension Method in C# ? Describe it in details.

Extension Method is a method that helps us to extend a class without creating a new derived class or without modifying the original class. Extension method works as a Static method but it is invoked with an instance of the extended class.
The Extension Method can be any Static Method which uses this keyword before its first parameter.
Only a class that has Static members and implements the this Keyword in the first parameter of a static method can use the extension methods.

Difference Between Static Method and Extension Method

  • Extension Method contains the this keyword before the first argument, whereas static method do not contain the this keyword in thier argument declaration
  • When an extension method is called, the arguments declared with this keyword is not passed whereas , while calling a static method , you must pass all the arguments
  • Extension Methods are defined in a Static Class only , but it is not necessary that the Static methods are declared in a Static Class only

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.