Abstraction is one of the key concepts of object oriented programming (OOP) used to manage complexity in .NET applications. By hiding the details of an implementation, abstraction simplifies a design and makes it easier to understand and maintain.
This programming tutorial talks about abstraction in C#, its benefits, how it can be implemented, and illustrates the concepts through code examples.
Before getting started, you may want to read our tutorial: C# Object Oriented Programming for Beginners if you need a primer on OOP basics.
What is Abstraction in C#?
Abstraction is a way of making something easier to use by hiding the complexity behind it. In the world of programming, abstraction is a powerful tool that can help make your code more readable and maintainable. Abstraction forms the core concept based on which certain OOP principles – such as the Open-Closed and Dependency Injection principles – have been designed.
Abstraction is a powerful tool in software development, and C# provides several different ways to achieve it. The most common form of abstraction is encapsulation,, which is the act of hiding implementation details from the user.
This can be done by creating classes and methods that are only accessible within the class or namespace where they are defined. C# also supports abstract classes and abstract interfaces, which provide a way to define contracts that must be implemented by derived types.
Abstraction exposes the necessary functionality to the external world while hiding or encapsulating the internal implementation details. Hence, programmers do not need to know the internal details of a complex logic, instead, they can just consume it.
Let us understand the concept of encapsulation and abstraction with an example. Consider a Car, its outer functionalities, and the mechanical inner details. While an automobile engineer is concerned with how the car works internally, a user is only concerned with how to drive the car, its mileage, etc. Hence, the perspectives differs from one user to another.
Read: Dependency Injection versus Inversion of Control
Here is another example of how abstraction works in real-life. Consider an online shopping cart application that has a login and register screen to register new users or login existing users. The internal details related to how the login process works, (i.e., the logic behind the login functionality), is abstracted from the end user.
The end user is only concerned with entering the user’s name and password. You, as the developer, would be concerned with how the login functionality will work, how to improve its performance, security, etc.
Benefits of Abstraction in C#
The following are the benefits of abstraction in C# at a glance:
- By isolating the application and the client code, abstraction can reduce the complexity involved in designing and implementing the software.
- Abstraction separates responsibilities into different entities (classes, methods, etc.)
- Abstraction enables the developer to alter the implementation details of a functionality without disturbing the interface which will be used by the end user to access and use the functionality
- Abstraction enhances code and data security since you only need to expose the relevant details to the end user
Types of Abstraction in C#
Basically, there are two types of abstraction in OOP: data abstraction and process abstraction. In the former data is hidden from the outside world with access to it provided by using certain access specifiers only; in the latter case the internal implementation details of the methods or operations are abstracted from the outside world.
In other words, data abstraction encapsulates data inside a single unit (usually a class) and makes this data accessible to the outside world only using the interfaces provided. Note that this is not the interface we know about in C#, instead, it represents the methods or operations that can be used to access the data.
You can learn more about C# classes in our tutorial: Introduction to Classes in C#.
How to Program Abstraction in C#
Here, we will show some examples of how to program abstraction in C#. Consider the following class named Customer:
public class Customer { private int _id; private string _firstName; private string _lastName; private string _email; private string _phone; private string _address; }
The Customer class contains several fields – or data members – that are private. So, if you create an instance of the Customer class, you will not be able to access any of these fields. Programmers need public members (methods or properties) to access these encapsulated fields, as shown below:
public int GetId() { return _id; } public string GetFirstName() { return _firstName; } public string GetLastName() { return _firstName; } public string GetEmail() { return _firstName; } public string GetPhone() { return _firstName; } public string GetAddress() { return _firstName; }
This is a classic example of data abstraction. Note that the data members are encapsulated inside the class, making them hidden from the outside world. The complete source code of the Customer class is given in the below code example:
public class Customer { private int _id; private string _firstName; private string _lastName; private string _email; private string _phone; private string _address; public int GetId() { return _id; } public string GetFirstName() { return _firstName; } public string GetLastName() { return _firstName; } public string GetEmail() { return _firstName; } public string GetPhone() { return _firstName; } public string GetAddress() { return _firstName; } }
To implement Process Abstraction, you can hide the internal implementation details of several methods that are part of an operation. For example, consider a method named Save, which saves data to the database. Internally, the method checks if the database is up and running, generates a unique Id for the new record, or checks if the data to be saved is valid using methods that are private to the external world. Developers just need to call the Save method and pass data to it without having to know how it would work internally.
Final Thoughts on Abstraction in C#
To make your C# code more maintainable and understandable, abstraction can be a powerful tool. By hiding the details of an implementation behind a well-defined interface, you can improve the readability of your code and make it simpler to change or extend the functionality of your program.
Looking to learn more C# coding principles? We have a list of some of the Best Online Courses to Learn C# Programming to help get you started.