C# Descending Keyword

LINQ (language integrated query)

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 and encourages elegant and clear code.

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

Example

Note

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.

The rules by which these clauses are transformed into method calls are not defined in the specification currently but they are fairly clear to follow. The orderby clause as shown in this example is translated into a method call to OrderByDescending.

OrderByDescending
Program that uses descending keyword [C#]

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

Descending keyword. The program text defines the Main entry point where control flow begins in the execution engine. The int[] array allocation occurs on the managed heap and the query expression is declared but not executed in the next statement.

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.

The C# programming language

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

Contextual Keyword

Ascending and descending

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 and so you do not need to specify ascending to get an ascending sort.

However, the ascending keyword can occasionally provide a symmetry to the orderby clauses in your query expressions when placed near clauses that stipulate the descending keyword.

Ascending Keyword OrderBy Clause.NET Framework information

Disassembling the C# program. When you use IL Disassembler to peek inside the compiled code of the C# program in this article, you will see that it is translated into a query method called OrderByDescending. This indicates that the descending contextual keyword, and all the query expression syntax, is simply turned into the more traditional method syntax before the program ever finishes the C# compilation step.

IL Disassembler Tutorial

Note: The execution engine only sees the intermediate language provided by the C# compiler so will have no way of detecting the original syntax.

Summary

We can use the descending keyword in the C# language 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 by the Common Language Runtime. However, the descending keyword improves the clarity and brevity of complex LINQ expressions at a high level.

LINQ Examples
.NET