C# Descending

Descending: sort order

A descending sort goes from high to low. For strings it goes from the last alphabetical string to the first. The descending contextual keyword provides this ability. It encourages elegant and clear code.

Contextual Keyword

Example

System.Linq namespace

We look at a program that specifies a query expression with the from, in, orderby, descending, and select keywords. The parts of query expressions that contain these keywords are called query clauses in the C# language specification.

Note:The rules by which these clauses are transformed into method calls are fairly clear to follow.

Next:The orderby clause here is translated into a method call to OrderByDescending.

OrderByDescending
C# program that uses descending keyword

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	//
	// An integer array that is in an arbitrary order.
	//
	int[] array = { 1, 3, 5, 7, 9 };
	//
	// Select the elements in a descending order with query clauses.
	//
	var result = from element in array
		     orderby element descending
		     select element;
	//
	// Evaluate the query and display the results.
	//
	foreach (var element in result)
	{
	    Console.WriteLine(element);
	}
    }
}

Output

9
7
5
3
1
Main method

In this example, control flow begins in the Main method. The int array allocation occurs on the managed heap. And the query expression is declared but not executed in the next statement.

Int Array

Finally, in the foreach-loop the query expression is evaluated and sorted. The result of the program is that the integer array elements are now ordered from the largest element value to the smallest element value.

Foreach

Contextual

Programming tip

The descending keyword is only considered a keyword in certain cases. The C# compiler provides a special-cased parser for query expressions and this is a main reason why they begin with the 'from' clause.

Query syntax was provided to allow for more natural syntax on declaration expressions. The descending contextual keyword here allows for a more natural language syntax for expressing the intent of the program.

Ascending

Arrow indicates looping

There is also an ascending contextual keyword that can be used in the same context as the descending keyword (following an orderby clause). Ascending sorts are normally the default in programming languages.

So:You do not need to specify ascending to get an ascending sort. Query expression sorts are implicitly ascending.

However:The ascending keyword can provide a symmetry to the orderby clauses in your query expressions.

Ascendingorderby

Internals

Collection abstraction: squares

When we use IL Disassembler to peek inside the compiled code, we see it is translated into a query method called OrderByDescending. All the query expression syntax is turned into traditional method syntax.

IL Disassembler

Note:The execution engine only sees the intermediate language provided by the C# compiler. It does not detect the original syntax.

Summary

The C# programming language

We used the descending keyword in a query expression based on LINQ. This keyword stipulates the progression of an orderby clause in a query expression. The query clause is translated to the method syntax before ever being executed.

Review:The descending keyword improves the clarity and brevity of complex LINQ expressions at a high level.


C#: LINQ