Nutshell Series

Object-Oriented Programming Concepts in C#

Object-Oriented Programming (OOP) helps us design applications using real-world concepts.
Below we explore important OOP principles and relationships in C#, along with examples.


Class

A Class is a blueprint that defines properties and methods.

public class Car
{
    public string Brand { get; set; }
    public void Drive()
    {
        Console.WriteLine($"{Brand} is driving.");
    }
}

Object

An Object is an instance of a class.

Car car1 = new Car { Brand = "Toyota" };
car1.Drive(); // Output: Toyota is driving.

Abstraction

Abstraction focuses on essential details while hiding the complexity.

public abstract class Payment
{
    public abstract void ProcessPayment(decimal amount);
}

public class CreditCardPayment : Payment
{
    public override void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Paid {amount} using Credit Card.");
    }
}

Encapsulation

Encapsulation hides implementation details using access modifiers.

public class BankAccount
{
    private decimal balance;

    public void Deposit(decimal amount) => balance += amount;
    public decimal GetBalance() => balance; // only controlled access
}

Polymorphism

Polymorphism allows the same method name to perform different tasks.

Overloading

public class Calculator
{
    public int Add(int a, int b) => a + b;
    public double Add(double a, double b) => a + b;
}

Overriding

public class Animal
{
    public virtual void Speak() => Console.WriteLine("Animal sound");
}

public class Dog : Animal
{
    public override void Speak() => Console.WriteLine("Bark");
}

Inheritance

Inheritance lets a class reuse properties and methods from a parent class.

public class Vehicle
{
    public void Start() => Console.WriteLine("Vehicle started");
}

public class Bike : Vehicle
{
    public void RingBell() => Console.WriteLine("Bell rings!");
}

Sealed Class

A sealed class cannot be inherited.

public sealed class Logger
{
    public void Log(string msg) => Console.WriteLine(msg);
}

Multiple Inheritance

C# does not allow multiple inheritance of classes, but interfaces provide it.

public interface IFly
{
    void Fly();
}

public interface ISwim
{
    void Swim();
}

public class Duck : IFly, ISwim
{
    public void Fly() => Console.WriteLine("Duck flying");
    public void Swim() => Console.WriteLine("Duck swimming");
}

Abstract Class

An abstract class cannot be instantiated directly; it must be inherited.

public abstract class Shape
{
    public abstract double Area();
}

public class Circle : Shape
{
    public double Radius { get; set; }
    public override double Area() => Math.PI * Radius * Radius;
}

Generalization

Generalization groups common features into a parent class.

public class Teacher : Person { }
public class Student : Person { }

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

Association

Association represents a relationship between classes.

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

public class Order
{
    public Customer OrderedBy { get; set; }
}

Aggregation

Aggregation is a weak “whole-part” relationship.

public class Department
{
    public List Employees { get; set; } = new List();
}

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

Composition

Composition is a strong “whole-part” relationship. If the whole is destroyed, parts are also destroyed.

public class House
{
    private Room room;
    public House()
    {
        room = new Room(); // Room cannot exist without House
    }
}

public class Room { }

Multiplicity

Multiplicity defines how many objects can participate in a relationship.

public class Library
{
    public List Books { get; set; } = new List();
}

public class Book { }

Interface

An interface defines a contract without implementation.

public interface INotifier
{
    void Send(string message);
}

public class EmailNotifier : INotifier
{
    public void Send(string message)
    {
        Console.WriteLine($"Email sent: {message}");
    }
}

Summary Table

Concept Definition Example
Class Blueprint of objects Car
Object Instance of a class car1 = new Car()
Abstraction Focus on essentials Payment (abstract)
Encapsulation Data hiding BankAccount with private balance
Polymorphism Many forms Calculator.Add() overloads, Dog.Speak() override
Inheritance Reuse parent properties Bike inherits Vehicle
Sealed Class Cannot be inherited Logger
Multiple Inheritance Via interfaces Duck : IFly, ISwim
Abstract Class Must be inherited Shape
Generalization Group common features Person parent for Student, Teacher
Association Relationship Order → Customer
Aggregation Weak whole-part Department → Employees
Composition Strong whole-part House → Room
Multiplicity How many objects Library → Books
Interface Contract without implementation INotifier
Nutshell Series

🔧 Dependency Injection (DI) Explained: Transient vs Scoped vs Singleton

Dependency Injection (DI) is a design pattern that simplifies how objects and their dependencies are managed in an application. Instead of classes creating their own dependencies, DI provides those dependencies from the outside. This makes applications cleaner, testable, and maintainable.

⚙️ What is Dependency Injection?

At its core, DI is about inversion of control: your classes don’t create what they need; a container provides them. This container decides:

  • How to create objects
  • When to reuse objects
  • How to dispose of them when no longer needed

📌 Service Lifetimes

When you register services in a DI container, you usually choose a lifetime:

  • Transient – A new instance is created every time it’s requested.
  • Scoped – One instance is created per request (or unit of work).
  • Singleton – A single instance is shared across the entire application lifetime.

🔄 Transient

A new instance is created each time the service is requested. Best for lightweight, stateless services.

// Example in C#
services.AddTransient<IEmailService, EmailService>();

📂 Scoped

A new instance is created once per request, but reused within that request. Useful for services like database contexts.

// Example in C#
services.AddScoped<IDbContext, AppDbContext>();

♾ Singleton

A single instance is created and reused for the entire lifetime of the application. Perfect for loggers, configuration readers, and caching providers.

// Example in C#
services.AddSingleton<ILogger, Logger>();

📊 When to Use Each?

  • Transient – For short-lived, stateless operations (e.g., helpers, formatters).
  • Scoped – For services tied to a single request or unit of work (e.g., DbContext).
  • Singleton – For shared state or expensive-to-create services (e.g., loggers, configuration, caching).

🚀 Conclusion

Dependency Injection ensures better code structure, easier testing, and improved flexibility.
Choosing the right lifetime — Transient, Scoped, or Singleton — helps you balance performance with resource management.

Nutshell Series

.NET Framework vs .NET Core: Key Differences

.NET Framework and .NET Core differ significantly in architecture, deployment, performance, and their support for modern development needs. Below is a detailed comparison covering both high-level and low-level aspects.

Key Differences

Feature .NET Framework .NET Core
Platform Support Windows only Cross-platform (Windows, Linux, macOS)
Performance Moderate due to legacy architecture Optimized for high performance with Just-In-Time (JIT) and Ahead-Of-Time (AOT) compilation improvements
Deployment Requires installation of .NET Framework on the system Can be deployed as a self-contained application without requiring .NET installation
Microservices Support Limited support due to monolithic architecture Designed for microservices with built-in support for containerization (Docker, Kubernetes)
Future Development No longer actively developed (last version .NET Framework 4.8) Actively maintained, evolved into .NET 8+
Runtime CLR (Common Language Runtime) with Just-In-Time (JIT) compilation CoreCLR with improved JIT and support for Ahead-Of-Time (AOT) compilation
APIs & Libraries Includes older Windows-specific APIs (e.g., WCF, Web Forms, Windows Workflow Foundation) Uses .NET Standard libraries and modern APIs with broader compatibility
Development Model Traditional Windows development with limited DevOps support Supports modern DevOps practices, CI/CD pipelines, and cloud-native applications

Low-Level Differences

  • Garbage Collection: .NET Core has an improved garbage collection mechanism, including low-latency GC modes suitable for high-performance applications.
  • Memory Management: .NET Core optimizes memory usage with Span and ValueTask to reduce allocations and improve efficiency.
  • Networking: .NET Core provides an enhanced networking stack with HTTP/2 support, asynchronous programming improvements, and lower latency.
  • Security Model: .NET Core follows a more modular security model with built-in support for modern authentication protocols (OAuth, OpenID Connect).
  • Threading and Concurrency: .NET Core enhances parallel processing with features like System.Threading.Channels and Task-based asynchronous patterns.

The choice between .NET Framework and .NET Core depends on your needs. If you’re maintaining legacy applications that rely on Windows-specific features, .NET Framework is suitable. However, for new, scalable, high-performance applications, .NET Core (or .NET 8+) is the best choice.

.Net, Azure, C#, Cloud Design Patterns, Nutshell Series, Technology and tricks

SOLID Principles (with c# examples) – Nutshell

SOLID Principles in .NET – Explained with Examples

SOLID Principles in .NET – Explained with Examples

The SOLID principles are five guidelines that help developers write clean, maintainable, and extensible object-oriented code.
Below we go through each principle with concise C# examples showing what is bad and what is good.


1. Single Responsibility Principle (SRP)

Rule: A class should have only one reason to change.

Bad Example

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

    public void SendEmail(string message)
    {
        // Logic to send email
    }
}

Here, User manages both data and email-sending logic. It has more than one responsibility.

Good Example

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

public class EmailService
{
    public void SendEmail(string email, string message)
    {
        // Logic to send email
    }
}

User now manages only user data, while EmailService handles emails. Each has one responsibility.


2. Open/Closed Principle (OCP)

Rule: Classes should be open for extension but closed for modification.

Bad Example

public class AreaCalculator
{
    public double CalculateArea(object shape)
    {
        if (shape is Circle c)
            return Math.PI * c.Radius * c.Radius;
        if (shape is Square s)
            return s.Side * s.Side;
        return 0;
    }
}

Adding a new shape forces us to modify AreaCalculator, violating OCP.

Good Example

public abstract class Shape
{
    public abstract double Area();
}

public class Circle : Shape
{
    public double Radius { get; set; }
    public override double Area() => Math.PI * Radius * Radius;
}

public class Square : Shape
{
    public double Side { get; set; }
    public override double Area() => Side * Side;
}

Each shape implements its own Area. Adding new shapes doesn’t require changing existing code.


3. Liskov Substitution Principle (LSP)

Rule: Derived classes should be substitutable for their base types without breaking behavior.

Bad Example

public class Bird
{
    public virtual void Fly() { }
}

public class Ostrich : Bird
{
    public override void Fly()
    {
        throw new NotImplementedException();
    }
}

Substituting Ostrich for Bird breaks behavior, since ostriches cannot fly.

Good Example

public abstract class Shape
{
    public abstract double Area();
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }
    public override double Area() => Width * Height;
}

Rectangle correctly substitutes Shape and can be used anywhere Shape is expected.


4. Interface Segregation Principle (ISP)

Rule: Don’t force classes to implement methods they don’t use.

Bad Example

public interface IMachine
{
    void Print();
    void Scan();
}

public class SimplePrinter : IMachine
{
    public void Print() { }
    public void Scan() { throw new NotImplementedException(); }
}

SimplePrinter doesn’t support scanning, yet it is forced to implement it.

Good Example

public interface IPrinter
{
    void Print();
}

public interface IScanner
{
    void Scan();
}

public class MultiFunctionPrinter : IPrinter, IScanner
{
    public void Print()
    {
        Console.WriteLine("Printing document...");
    }

    public void Scan()
    {
        Console.WriteLine("Scanning document...");
    }
}

Now classes only implement what they need. A simple printer just implements IPrinter.


5. Dependency Inversion Principle (DIP)

Rule: High-level modules should not depend on low-level modules. Both should depend on abstractions.

Bad Example

public class DataManager
{
    private readonly SQLDatabase _db = new SQLDatabase();

    public void Save(string data)
    {
        _db.SaveData(data);
    }
}

public class SQLDatabase
{
    public void SaveData(string data) { }
}

DataManager is tightly coupled to SQLDatabase, making it hard to switch databases.

Good Example

public interface IDatabase
{
    void SaveData(string data);
}

public class SQLDatabase : IDatabase
{
    public void SaveData(string data)
    {
        // Save data to SQL database
    }
}

public class DataManager
{
    private readonly IDatabase _database;
    public DataManager(IDatabase database)
    {
        _database = database;
    }

    public void Save(string data)
    {
        _database.SaveData(data);
    }
}

DataManager now depends on IDatabase. Any database implementation can be injected.


Key Takeaways

  • SRP — One class, one responsibility.
  • OCP — Extend, don’t modify working code.
  • LSP — Subtypes must preserve base-class behavior.
  • ISP — Keep interfaces small and focused.
  • DIP — Depend on abstractions, not concretions.