TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

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.

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

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

Ascendingorderby

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf