Introducing LINQ (3) Programming Paradigms

[LINQ via C#] - [Introducing LINQ]

Programming paradigm is the fundamental style of programming.

Imperative vs. declarative


This is the fore mentioned example of collection processing:

public partial class Imperative
    public static List<Person> FilterAndOrderByAge(IEnumerable<Person> source)
        List<Person> results = new List<Person>();
        foreach (Person person in source)
            if (person.Age >= 18)

        Comparison<Person> personComparison = delegate(Person a, Person b)
                int ageComparison = 0 - a.Age.CompareTo(b.Age);
                return ageComparison != 0
                    ? ageComparison
                    : string.Compare(a.Name, b.Name, StringComparison.Ordinal);

        return results;

and the same processing with LINQ:

public partial class LinqToObjects
    public static IEnumerable<Person> FilterAndOrderByAge(IEnumerable<Person> source)
        return from person in source
               where person.Age >= 18
               orderby person.Age descending, person.Name
               select person;

Their styles/paradigms are very different:

Imperative and declarative programming paradigms are different philosophies:

  • Imperative paradigm is about thinking from bottom up. It explicitly provides each action to be taken, and a sequence of action can be a “bigger” action, and so on. Computation is to execute these actions.
    • Object oriented programming of C# is a typical imperative paradigm.
  • Declarative paradigm is about thinking from top down. It is higher level, more abstract, has clear correspondence to mathematical logic, where can be considered as theories of a formal logic, and computations can be considered as deductions in that logic space. As a higher level and more abstract paradigm, it usually minimize or eliminate side effects.


As fore mentioned, LINQ is SQL-like. The following SQL query is very declarative:

SELECT [ProductName], [UnitPrice] FROM [Products] ORDER BY [UnitPrice] DESC


Another declarative example is XAML. Compare C#:

Button button = new Button();
button.Content = "Submit";
button.HorizontalAlignment = HorizontalAlignment.Left;
button.VerticalAlignment = VerticalAlignment.Top;
button.Margin = new Thickness(10, 10, 0, 0);
button.Width = 75;
button.Click += this.Submit;

with  the following XAML:

<Button Content="Submit" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Width="75" Click="Submit" />

Above C# is imperative and above XAML is declarative.


Another controversial topic is HTML. In CLR via C# 2nd edition, Jeffrey Richter said (This paragraph is removed in the 3rd edition),

An example of declarative programming is when a person creates a text file and explicitly enters HTML tags into the file by using an editor such as Notepad.exe. In this scenario, the HTML tags act as instructions that are eventually processed by the Internet browser so that it can lay out the page in a window. The HTML tags are declaring how the program (Web page) should be displayed and operate, and it's the programmer who decides what tags to use and where. Many hard-core programmers don't consider HTML programming to be real programming, but I do.

Similar to C# vs. XAML, if comparing with JavaScript:

var img = document.CreateElement("img");
img.src = ""; = "300px"; = "200px";
img.title = "Microsoft Way";

with HTML:

<img src="" style="width: 300px; height: 200px;" title="Microsoft Way" />

then HTML is the declarative.

Programming paradigms and languages

Imperative, declarative, object-oriented, functional, ... There are many paradigms for programming. The common paradigms can be categorized as:

One programming language can adopt several paradigms. Take C as an example:

Another example is JavaScript:

  • JavaScript is imperative by default, it is 
    • Procedural
    • Prototype-based
  • It is also elegantly functional

And finally, C# is:

  • Declarative (attribute, regular expression, data annotation, code contracts, …) 
    • Reactive (Rx)
    • Functional (lambda expression, higher-order function, LINQ, …)
  • Event-driven (event)
  • Generic
  • Imperative (by default)
    • Class-based Object-oriented (class)
    • Procedural (static class, static method, using static)
  • Metaprogramming (code DOM, expression tree, IL emit, compiler as a service)
    • Reflective (reflection)
  • Parallel (TPL, Parallel LINQ)
  • Structured
    • Aspect-oriented (Unity)

Thanks to Microsoft and Anders Hejlsberg, C#/.NET is powerful and productive, work in many different scenarios.

Declarative C#

C# 3.0+ introduced a lot of syntax to make it more declarative. For example, the object initializer collection initializer:

List<Person> team = new List<Person>();
Person anna = new Person();
anna.Age = 25;
anna.Name = "Anna";
Person bob = new Person();
bob.Age = 30;
bob.Name = "Bob";
Person charlie = new Person();
charlie.Age = 35;
charlie.Name = "Charlie";
Person dixin = new Person();
dixin.Age = 30;
dixin.Name = "Dixin";

Comparing to:

List<Person> team = new List<Person>
        new Person() { Age = 25, Name = "Anna" }, 
        new Person() { Age = 30, Name = "Bob" }, 
        new Person() { Age = 35, Name = "Charlie" }, 
        new Person() { Age = 30, Name = "Dixin" }, 

the first code snippet is more imperative, and the second is more declarative. Actually, there are many other declarative aspects in C# programming. 


Actually, declarative programming in C# is not something brand new. C# has attributes from the beginning:

public class HomeController : Controller
    public ActionResult Index()
        return this.View();

Regular expression

Regular expressions can be considered declarative:

namespace System.ComponentModel.DataAnnotations
    using System.Text.RegularExpressions;

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
    public sealed class EmailAddressAttribute : DataTypeAttribute
        private static readonly Regex emailRegex = new Regex(
            RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled);

        public EmailAddressAttribute()
            : base(DataType.EmailAddress)
            this.ErrorMessage = DataAnnotationsResources.EmailAddressAttribute_Invalid;

        public override bool IsValid(object value)
            if (value == null)
                return true;

            string text = value as string;
            return text != null && emailRegex.Match(text).Length > 0;

Data annotation

Data Annotation is intuitively declarative:

public class Person
    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = nameof(Resources.NameRequired))]
    [StringLength(1, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = nameof(Resources.InvalidName))]
    public string Name { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = nameof(Resources.AgeRequired))]
    [Range(0, 123, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = nameof(Resources.InvalidAge))] //
    public int Age { get; set; }

    [EmailAddress(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = nameof(Resources.InvalidEmail))]
    public string Email { get; set; }

Code contracts

C# 3.0 and 4.0 introduced code contracts, which is also declarative:

public class Person
    private readonly string name;

    private readonly int age;

    public Person(string name, int age)
        Contract.Requires<ArgumentOutOfRangeException>(age >= 0); = name;
        this.age = age;

    public string Name


    public int Age
            Contract.Ensures(Contract.Result<int>() >= 0);

            return this.age;

LINQ and Functional C#

Above LinqToObjects.FilterAndOrderByAge method implementation is equivalent to (actually is compiled to):

public partial class LinqToObjects
    public static IEnumerable<Person> FilterAndOrderByAge(IEnumerable<Person> source)
        return source
            .Where(person => person.Age >= 18)
            .OrderByDescending(person => person.Age)
            .ThenBy(person => person.Name);

This LINQ to Objects program is functional, and purely functional:

  • Type inference
  • Extension method
  • Lambda expression as anonymous function
  • Higher order function
  • Query expression/Query method

Since C# 3.0, more and more language features has been added to C#, which make C# more and more functional. Besides above features, there are more:

  • Auto property, auto property initializer, getter only auto property
  • Object initializer, collection initializer, index initializer, extension Add in collection initializers
  • Anonymous type
  • Partial class, partial interface, partial method
  • Lambda expression as expression tree, expression-bodied members
  • Async lambda expression
  • Covariance and contravariance

Next, these language features will be explained in detail.


Add a Comment

As it will appear on the website

Not displayed

Your website