Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# Descending Keyword

This C# example program uses the descending keyword in a query expression. It requires System.Linq.

Descending. 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.

Example. 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.


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)



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.


Contextual. 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. 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.


Internals. 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. Descending is used in query expressions. 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.