Channel: t.me/brain_bytes
# Programming Paradigms vs Programming Model
## 1) What Is a Programming Paradigm?
A programming paradigm is a conceptual style or philosophy for thinking about problems and structuring solutions in code.
It answers questions like:
- How do we view data? (objects, pure values, sequences, events)
- How do we express behavior? (methods, functions, queries, handlers)
- How do system parts interact? (method calls, message passing, events, streams)
Paradigms guide how you mentally model the domain. They are about “How should I think and organize?” not about specific syntax.
Examples: Imperative, Object-Oriented (OOP), Functional, Declarative, Event-Driven, Asynchronous, Reactive.
## 2) What Is a Programming Model?
A programming model is the concrete set of language mechanisms, runtime behavior, keywords, and APIs that let you implement one or more paradigms.
In C#, the programming model provides:
- OOP constructs:
class, interface, abstract, virtual, override - Functional/declarative tools: LINQ (
Where, Select, query syntax), lambdas, delegates (Func<>, Action), expression trees, record types - Asynchronous constructs:
async, await, Task, ValueTask, CancellationToken - Event-driven features:
event, EventHandler, delegates - Memory/execution semantics: managed runtime, garbage collection, value vs reference types
In short:
Paradigm = conceptual lens (thinking model)
Programming model = toolbox + mechanics enabling that lens
---
## 3) Major Paradigms with C# Examples
### A) Imperative Paradigm
Focus: Explicit step-by-step instructions and mutable state.
int sum = 0;
int[] numbers = { 3, 5, 7, 9 };
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
Console.WriteLine(sum);
Characteristics: loops, assignments, control flow (
if, for, while).---
### B) Object-Oriented Programming (OOP)
Model the world as objects combining state + behavior.
public class Car
{
public string Brand { get; }
public int Speed { get; private set; }
public Car(string brand, int speed)
{
Brand = brand;
Speed = speed;
}
public void Accelerate(int amount) => Speed += amount;
public override string ToString() => $"{Brand} => {Speed} km/h";
}
var car = new Car("BMW", 120);
car.Accelerate(30);
Console.WriteLine(car); // BMW => 150 km/h
Core concepts: Encapsulation, Inheritance, Polymorphism, Abstraction.
Polymorphism example:
public abstract class Shape
{
public abstract double Area();
}
public class Circle : Shape
{
public double Radius { get; }
public Circle(double r) => Radius = r;
public override double Area() => Math.PI * Radius * Radius;
}
public class Rectangle : Shape
{
public double Width { get; }
public double Height { get; }
public Rectangle(double w, double h) { Width = w; Height = h; }
public override double Area() => Width * Height;
}
Shape[] shapes = { new Circle(3), new Rectangle(4, 5) };
foreach (var s in shapes)
Console.WriteLine(s.Area());
---
### C) Functional Style (in C#)
Emphasis on pure transformations, minimized mutation, composability.
int[] numbers = { 3, 5, 7, 9 };
int sum = numbers.Aggregate((a, b) => a + b);
var squaredEvens = numbers
.Where(n => n % 2 == 0)
.Select(n => n * n)
.ToList();
Console.WriteLine(sum);
Console.WriteLine(string.Join(", ", squaredEvens));Traits: Lambdas, LINQ pipelines, declarative transformations, easier testing.
---
### D) Declarative Paradigm
Describe WHAT you want, not HOW to iterate.
var products = new[]
{
new { Name = "Laptop", Price = 45000 },
new { Name = "Mouse", Price = 400 },
new { Name = "Monitor", Price = 9000 },
};
var expensive =
from p in products
where p.Price > 5000
orderby p.Price descending
select p.Name;
Console.WriteLine(string.Join(" | ", expensive));
You specify filters and projections; LINQ decides iteration strategy (and can translate to SQL via EF Core).
---