Ads

C# Programming 11 - Do you know?, with declarative paradigm we can write less code compare to imperative paradigm

Hello All,

In this article we will see the difference between declarative and imperative paradigm in programming?


A programming paradigm is a fundamental style of computer programming.

Imperative Paradigm:

It is also called as writing traditional style of coding.

I mean, With imperative programming, we tell the compiler that what we want to happen, step by step.

Lets say...

I have an integer number collection, from this collection we need to get the odd numbers.

List<int> myCollection = new List<int> { 11, 24, 35, 44, 56, 128 };

We will do following steps.

1. Create a integer result collection

List<int> resultsCollection = new List<int>();

2. Step through each number in the collection and then finally Check the number, if it's odd, add it to the results or print it

foreach(var num in myCollection)
{
    if (num % 2 != 0)
          resultsCollection.Add(num);
}

Here is the complete code and result


Declarative paradigm:

Here we write a code that describes what we need, It is not necessary that how to get it.

In another word Declarative paradigm expresses the logic of a computation(What do) without describing its control flow(How do).  

It helps us to hide complete implementation and code becomes very shorter.

Let see same example...

List<int> myCollection = new List<int> { 11, 24, 35, 44, 56, 128 };

var resultsCollection = myCollection.Where( num => num % 2 != 0);

System.Console.WriteLine(".............ODD NUMBERS ARE.............");

foreach(var item in resultsCollection){
    System.Console.WriteLine(item);
}

That's it for this article, I hope you enjoyed, see you in next article until then take care bye see you.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !