
Programming paradigms influence the way developers think about and write code. Two of the most essential styles are imperative and declarative programming. Gaining clarity on these paradigms and knowing when to use each can greatly improve your efficiency and coding approach. In this article, we’ll explore the key differences between imperative and declarative programming, using practical examples in C# to showcase the declarative style in action, and highlight features such as LINQ.
Imperative Programming
Imperative programming is a programming style where the developer writes code that describes how a task should be performed, step by step. It focuses on changing the program’s state through statements like loops, conditionals, and variable assignments. In C#, examples include using `for` loops or `if-else` blocks to control the flow of execution. This approach gives you full control over the logic and state changes.
Key Features of Imperative Programming
- Control Flow: The programmer explicitly defines the flow of execution using loops (for, while), conditionals (if-else), and function calls.
- State Changes: Relies on mutable variables and step-by-step updates to manage state throughout the program.
- Language Support: Commonly used in languages like C, C++, and also supported in C# through traditional control structures.
Imperative Programming Example with C# Code
Let’s consider a scenario where we have a list of students, each with a set of subject-wise marks. Our goal is to calculate the total marks for each student. Using imperative programming, we’ll write step-by-step instructions to loop through the data, accumulate the marks, and print the result for each student.
public class ImperativeExample
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
students.Add(new Student()
{
StudentId = 1,
StudentName = "Student 1",
studentMarks = new List<StudentMarks>()
{
new StudentMarks() { SubjectId = 1, Mark = 30 },
new StudentMarks() { SubjectId = 2, Mark = 50 },
new StudentMarks() { SubjectId = 3, Mark = 70 },
new StudentMarks() { SubjectId = 4, Mark = 65 },
new StudentMarks() { SubjectId = 5, Mark = 80 }
}
});
students.Add(new Student()
{
StudentId = 2,
StudentName = "Student 2",
studentMarks = new List<StudentMarks>()
{
new StudentMarks() { SubjectId = 1, Mark = 70 },
new StudentMarks() { SubjectId = 2, Mark = 90 },
new StudentMarks() { SubjectId = 3, Mark = 70 },
new StudentMarks() { SubjectId = 4, Mark = 95 },
new StudentMarks() { SubjectId = 5, Mark = 80 }
}
});
foreach (var item in students)
{
int total = 0;
for (int i = 0; i < item.studentMarks.Count; i++)
{
total += item.studentMarks[i].Mark;
}
Console.WriteLine($"Student: {item.StudentName} || Total Marks: {total}");
}
Console.ReadKey();
}
}
Output

Declarative Programming
Declarative programming is a style where the developer describes the desired outcome, rather than detailing the exact steps to achieve it. It focuses on expressions and logic, leaving control flow to the language or framework. In C#, declarative code often utilizes features like LINQ, which enables data transformation or querying in a concise and readable manner. This approach promotes cleaner and more maintainable code.
Key Features of Declarative Programming
- High-Level Constructs: Focuses on writing what the program should accomplish using expressions and declarations, rather than explicit step-by-step instructions.
- Abstracted Control Flow: The control flow is managed by the language or framework, not the programmer.
- Common Languages/Technologies: Widely seen in SQL, HTML, CSS, and functional programming languages like Haskell. In C#, LINQ is a strong example of a declarative approach.
Declarative Programming Example with C# Code
Now, let’s achieve the same goal by calculating total marks for each student using a declarative approach. Instead of manually looping and updating totals, we’ll use LINQ in C# to express what we want: the sum of each student’s marks. The underlying logic, like iteration and aggregation, is handled by the framework, resulting in cleaner and more concise code.



Join the conversation! Your thoughts help the community grow.