Consider now this program. We have a string array of 3 values. We use a query expression (starting with "from") to sort the values.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
var animals = new string[] {
"bird",
"cat",
"ant" };
var result = from animal in animals
orderby animal ascending
select animal;
// Our result is an IOrderedEnumerable.
// ... We can use it as an IOrderedEnumerable.
Test(result);
// We can use the IOrderedEnumerable as an IEnumerable.
Test2(result);
// We can use extension methods for IEnumerable on IOrderedEnumerable.
Console.WriteLine(
"Count: " + result.Count());
}
static void Test(IOrderedEnumerable<string> result)
{
// We can loop over an IOrderedEnumerable.
foreach (string value in result)
{
Console.WriteLine(
"IOrderedEnumerable: " + value);
}
}
static void Test2(IEnumerable<string> result)
{
foreach (string value in result)
{
Console.WriteLine(
"IEnumerable: " + value);
}
}
}
IOrderedEnumerable: ant
IOrderedEnumerable: bird
IOrderedEnumerable: cat
IEnumerable: ant
IEnumerable: bird
IEnumerable: cat
Count: 3