Understanding Key Object-Oriented Programming (OOP) Concepts in C#

Kevin Peery
7 min readSep 25, 2023

--

Classes and Objects

At the core of OOP in C# are classes and objects:

  • Class: A class is a blueprint or template for creating objects. It defines the structure and behavior of objects of that class.
  • Object: An object is an instance of a class. It has attributes (properties) and behaviors (methods) defined by the class.

Here’s an example:

// Define a class
class Car
{
public string Make;
public string Model;

public void StartEngine()
{
Console.WriteLine("Engine started!");
}
}

// Create an object (instance) of the class
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Camry";
myCar.StartEngine();

In this example, Car is a class, and myCar is an object of that class.

Inheritance

Inheritance allows you to create new classes that reuse properties and behaviors of existing classes. It establishes a hierarchy of classes, where a subclass inherits from a superclass:

class Animal
{
public string Name;
public void Eat() { /* Eating logic */ }
}

class Dog : Animal
{
public void Bark() { /* Barking logic */ }
}

Dog myDog = new Dog();
myDog.Name = "Fido";
myDog.Eat(); // Inherited method
myDog.Bark(); // Dog-specific method

In this example, Dog is a subclass of Animal, inheriting the Name attribute and Eat method from the superclass.

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. This promotes flexibility in method invocation, depending on the actual type of the object at runtime:

class Shape
{
public virtual void Draw()
{
// Default drawing logic
}
}

class Circle : Shape
{
public override void Draw()
{
// Circle-specific drawing logic
}
}

Shape myShape = new Circle();
myShape.Draw(); // Calls Circle's Draw method

In this example, myShape is an object of type Shape, but it references a Circle object. At runtime, the Draw method of Circle is invoked.

Abstraction

Abstraction is the process of hiding complex implementation details while exposing essential features. It allows you to focus on high-level aspects of an object:

abstract class Shape
{
public abstract void Draw(); // Abstract method with no implementation
}

In this case, Shape is an abstract class with an abstract method Draw. Concrete subclasses must provide their own implementations.

Encapsulation

Encapsulation bundles data (attributes) and methods (functions) that operate on that data into a single unit (class). It restricts direct access to some components, providing data hiding and protecting the object’s state:

class BankAccount
{
private decimal balance;

public void Deposit(decimal amount)
{
if (amount > 0)
balance += amount;
}

public decimal GetBalance()
{
return balance;
}
}

Here, balance is a private field, and methods are used to access and modify it, ensuring proper encapsulation.

Interfaces

Interfaces define contracts that classes must adhere to by implementing specific methods and properties. They enable multiple classes to share common functionality while allowing each class to have its own implementation:

interface IDrawable
{
void Draw();
}

class Circle : IDrawable
{
public void Draw()
{
// Circle drawing logic
}
}

In this example, Circle implements the IDrawable interface by providing its own Draw method.

Composition

Composition is a principle that emphasizes building complex objects or behaviors by combining simpler objects, rather than relying solely on inheritance. It promotes flexibility and reusability by allowing you to create new classes by composing existing classes.

Here’s a brief explanation and an example of composition:

Composition:

Composition involves creating classes that are composed of other classes or objects as parts, and it often follows the “has-a” relationship.

class Engine
{
public void Start()
{
Console.WriteLine("Engine started.");
}
}

class Car
{
private Engine engine;

public Car()
{
engine = new Engine();
}

public void StartCar()
{
engine.Start();
Console.WriteLine("Car started.");
}
}

In this example, the Car class has a StartCar method that internally uses an Engine object. This demonstrates composition because the Car class is composed of an Engine object to achieve its functionality.

LINQ (Language Integrated Query)

LINQ, or Language Integrated Query, is a feature in C# that provides a powerful way to query and manipulate data within collections, databases, and other data sources. It allows you to write queries using a familiar and expressive syntax, making data manipulation more readable and maintainable.

LINQ queries are typically used with collections, such as arrays, lists, or dictionaries. Here’s a basic example of LINQ in action:

using System;
using System.Linq;
using System.Collections.Generic;

public class LINQExample
{
public static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Using LINQ to filter even numbers
var evenNumbers = numbers.Where(n => n % 2 == 0);

// Using LINQ to project the result into a new list
var squaredNumbers = numbers.Select(n => n * n);

Console.WriteLine("Even Numbers: " + string.Join(", ", evenNumbers));
Console.WriteLine("Squared Numbers: " + string.Join(", ", squaredNumbers));
}
}

In this example, LINQ is used to filter even numbers and to project the numbers into their squared values. LINQ allows us to work with data in a more declarative and expressive manner.

Reflection

Reflection is a feature in C# that enables you to inspect and interact with the metadata and behavior of types, objects, and assemblies at runtime. It provides a way to query and manipulate classes, methods, properties, fields, and more, making it a valuable tool for tasks such as dynamic object creation, serialization, and custom attribute usage.

Here’s a basic example of how reflection can be used to inspect and interact with types:

using System;
using System.Reflection;

public class ReflectionExample
{
public static void Main(string[] args)
{
// Get the type of a class
Type carType = typeof(Car);

// Get public properties of the class
PropertyInfo[] properties = carType.GetProperties();

Console.WriteLine("Properties of the Car class:");
foreach (var property in properties)
{
Console.WriteLine(property.Name);
}

// Invoke a method dynamically
MethodInfo startEngineMethod = carType.GetMethod("StartEngine");
Car myCar = new Car();
startEngineMethod.Invoke(myCar, null);
}
}

public class Car
{
public string Make { get; set; }
public string Model { get; set; }

public void StartEngine()
{
Console.WriteLine("Engine started!");
}
}

In this example, reflection is used to:

  • Get the type of the Car class.
  • Retrieve and display the public properties of the Car class.
  • Dynamically invoke the StartEngine method of the Car class.

Entity Framework

Entity Framework (EF) is a powerful Object-Relational Mapping (ORM) framework in C# that simplifies database interactions by allowing developers to work with databases using object-oriented code. It bridges the gap between the object-oriented world of C# and the relational world of databases.

Key aspects of Entity Framework include:

  • Entity Classes: Entity Framework allows you to define entity classes that represent database tables. These classes map database tables to C# objects.
  • DbContext: The DbContext class acts as the entry point to the Entity Framework. It manages the database connection, tracks changes to entities, and facilitates database operations.
  • LINQ Integration: Entity Framework integrates seamlessly with LINQ, enabling developers to query and manipulate data using a familiar and expressive syntax.

Here’s a basic example of using Entity Framework to define an entity class and perform database operations:

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;

// Define an entity class
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}

// Define a DbContext
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}

public class EntityFrameworkExample
{
public static void Main(string[] args)
{
using (var context = new AppDbContext())
{
// Create a new product
var product = new Product
{
Name = "Laptop",
Price = 999.99M
};

// Add the product to the database
context.Products.Add(product);
context.SaveChanges();

// Retrieve and display products
var products = context.Products.ToList();
foreach (var p in products)
{
Console.WriteLine($"Product: {p.Name}, Price: {p.Price:C}");
}
}
}
}

In this example, Entity Framework is used to define an entity class (Product), create a database context (AppDbContext), and perform database operations, including adding a new product and querying products using LINQ.

CI/CD and OOP: A Synergistic Approach

Object-Oriented Programming (OOP) principles play a vital role in modern software development, providing a foundation for building maintainable and scalable code. But how do these principles fit into the broader context of software development practices such as Continuous Integration and Continuous Deployment (CI/CD)?

Encapsulation and CI/CD

Encapsulation, one of the core OOP principles, emphasizes bundling data and methods into a single unit (class). This concept aligns well with CI/CD practices, where code changes must be encapsulated within version control repositories and automated build pipelines. By encapsulating code changes, developers can ensure that new features or bug fixes are isolated, tested, and integrated smoothly into the project.

Abstraction and CI/CD

Abstraction in OOP involves hiding complex implementation details while exposing essential features. Similarly, CI/CD abstracts the complexities of the deployment process by automating steps like building, testing, and deployment. Abstraction in CI/CD streamlines the process, making it more manageable and less error-prone.

Polymorphism and CI/CD

Polymorphism allows objects of different classes to be treated as objects of a common base class. In CI/CD, different environments (development, staging, production) often require different configurations or settings. Polymorphism-like techniques, such as environment-specific configuration files or settings, enable code to adapt seamlessly to various deployment targets.

While this article primarily focuses on understanding OOP principles in C#, it’s essential to recognize that these principles aren’t isolated concepts. They are integral to building robust and maintainable software, whether you’re working on local development or contributing to a CI/CD pipeline. By applying OOP principles thoughtfully, you can create code that integrates smoothly into the CI/CD workflow, contributing to a more efficient and reliable software development process.

Conclusion

Understanding these key OOP concepts is essential for writing clean, maintainable, and extensible code in C#. Whether you’re preparing for an interview or looking to enhance your coding skills, mastering these principles will serve you well in your software development journey. Practice applying these concepts to real-world scenarios to solidify your understanding and become a proficient C# developer.

--

--