Skip to content

Mastering Object-Oriented Programming (OOP) in C#: Benefits and Code Examples - Pedro Constantino

Object-Oriented Programming (OOP) is a programming paradigm that uses “objects” to design and structure software. It’s a foundational concept in many modern programming languages, including C#. Understanding OOP is crucial for developing...

00:00 / --:--

Read by your browser's built-in voice.

Object-Oriented Programming (OOP) is a programming paradigm that uses “objects” to design and structure software. It’s a foundational concept in many modern programming languages, including C#. Understanding OOP is crucial for developing scalable, maintainable, and efficient applications. Let’s explore the core principles of OOP and their benefits, followed by a practical code example in C#.

1. Encapsulation

Definition: Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, or class. It restricts direct access to some of an object’s components, which means that internal implementation details are hidden from the outside world.

Benefit: Encapsulation promotes modularity and reduces complexity. By hiding the internal workings of objects and exposing only what’s necessary, it allows you to protect the integrity of an object’s state and enforce clear interfaces.

Access control by modifiers (private, public, etc.)

Example:

C#

public class AccountBanking
{
	private decimal balance;
	
	public void Deposit(decimal value) => balance += value;
	
	public decimal CheckBalance() => balance;
}

2. Inheritance

Definition: Inheritance allows a class (child class) to inherit properties and behavior (methods) from another class (parent class). It enables the creation of a new class that reuses, extends, or modifies the behavior of an existing class.

Benefit: Inheritance promotes code reuse and establishes a natural hierarchy between classes, which can make your code more intuitive and easier to manage.

In the example we can see that we have access to the “name” property

Example:

C#

public class User
{
	public string Name { get; set; }
}

public class Administrator : User
{
	public void ManageSystem() => Console.WriteLine($"{Name} managing system.");
}		

3. Polymorphism

Definition: Polymorphism allows objects of different classes to be treated as objects of a common super class. It enables a single interface to represent different underlying forms (data types).

Benefit: Polymorphism enhances flexibility and integration. It allows for the design of more generic and reusable code, making it easier to introduce new functionality without altering existing code.

Example: 

C#

public abstract class Notification
{
	public abstract void Enviar(string message);
}

public class EmailNotification : Notification
{
	public override void Enviar(string message) => Console.WriteLine($"Email: {message}");
}

public class SmsNotification : Notification
{
	public override void Enviar(string message) => Console.WriteLine($"SMS: {message}");
}		

4. Abstraction

Definition: Abstraction involves hiding the complex implementation details of a system and exposing only the essential features. It focuses on what an object does rather than how it does it.

Benefit: Abstraction reduces complexity by allowing the programmer to focus on interactions at a higher level, ignoring the underlying implementation details. This improves code clarity and maintainability.

Example:

C#

public interface IPaymentProcessor
{
	void Process(decimal value);
}

public class CardPayment : IPaymentProcessor
{
	public void Process(decimal value) => Console.WriteLine($"Processing payment{value:C}");
}
			

Conclusion

Object-Oriented Programming is a powerful paradigm for creating modular, reusable, and maintainable code. By mastering encapsulation, inheritance, polymorphism, and abstraction, you can develop more effective C# applications. Understanding these principles ensures your systems remain robust and adaptable to change.

Rate this article

Rate this article out of 5

5,0 / 5 · 1 rating

Selecting a star will ask you to sign in.
HV
Huy Vũ
Contributor at Selectgo

Shares practical engineering notes on this blog.