LINQ Reverse Method

LINQ Reverse Method in C# with Examples

In this article, I am going to discuss the LINQ Reverse Method in C# with examples. Please read our previous article before proceeding to this article where we discussed LINQ ThenBy and ThenByDescending Method in C# with some examples. As part of this article, we are going to discuss the following pointers in details.

  1. What is Reverse Method?
  2. Different Types of Example using both Method and Query Syntax.
  3. We will discuss the Reverse Method available in System.LInq and System.Collections.Generic namespaces.
  4. Finally, we will discuss how to apply the LINQ Reverse method (the Reverse Method which is available in System.LInq namespace) on a collection of List<T> type.
What is Reverse Method in C#?

The LINQ Reverse method is used to reverse the data stored in a data source. That means this method will not change the data rather it simple reverse the data stored in the data source. So, as a result, we will get the output in reverse order.

C# Reverse Method Signature:

The Reverse Method is implemented in two different namespaces such as System.LInq and System.Collections.Generic namespaces. Let us have a look at the signature or definition of the Reverse Method which is shown in the below image.

LINQ Reverse Method in C# Definition

As you can see the Reverse method which belongs to System.Linq namespace implemented as an extension method on IEnumerable<TSource> interface and more importantly this method also return an IEnumerable<TSource> type.

On the other hand, the Reverse method which belongs to the System.Collections.Generic namespace is not returning any value as the return type is void.

With this keep in mind, let us see some example for a better understanding of the Reverse method.

Example1: System.Linq namespace Reverse method

In order to understand this, we are going to work with an integer array as shown in the below example.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArray = new int[] { 10, 30, 50, 40,60,20,70,100 };
            Console.WriteLine("Before Reverse the Data");
            foreach (var number in intArray)
            {
                Console.Write(number + " ");
            }
            Console.WriteLine();

            IEnumerable<int> ArrayReversedData = intArray.Reverse();
            Console.WriteLine("After Reverse the Data");
            foreach (var number in ArrayReversedData)
            {
                Console.Write(number + " ");
            }
            
            Console.ReadKey();
        }
    }
}

Output:

System.Linq Reverse Method in C#

Note: In the above example, if you go to the definition of the Reverse method then you can see that this Reverse method belongs to the System.Linq namespace and hence we can able to store the data in a variable of type IEnumerable<int> as the source contains integer data.

Using Query Syntax:

The most important point is that we do not have any such operator called Reverse available in Linq to write query syntax. So here we need to use the mixed syntax. So let us see how to rewrite the same example using mixed syntax.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArray = new int[] { 10, 30, 50, 40,60,20,70,100 };
            Console.WriteLine("Before Reverse the Data");
            foreach (var number in intArray)
            {
                Console.Write(number + " ");
            }
            Console.WriteLine();

            IEnumerable<int> ArrayReversedData = (from num in intArray
                                                  select num).Reverse();

            Console.WriteLine("After Reverse the Data");
            foreach (var number in ArrayReversedData)
            {
                Console.Write(number + " ");
            }
            
            Console.ReadKey();
        }
    }
}
Example2: System.Collections.Generic namespace Reverse method

In order to understand the above Reverse method, we need to create a collection of List<string> as shown in the below examples.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> stringList = new List<string>() { "Preety", "Tiwary", "Agrawal", "Priyanka", "Dewangan"};
            
            Console.WriteLine("Before Reverse the Data");
            foreach (var name in stringList)
            {
                Console.Write(name + " ");
            }
            Console.WriteLine();

            //You cannot store the data like below as this method belongs to
            //System.Collections.Generic namespace whose return type is void
            //IEnumerable<int> ArrayReversedData = stringList.Reverse();

            stringList.Reverse();
            Console.WriteLine("After Reverse the Data");
            foreach (var name in stringList)
            {
                Console.Write(name + " ");
            }

            Console.ReadKey();
        }
    }
}

If you go to the definition of the above Reverse method, then you can see this method belongs to the System.Collections.Generic namespace and the return type of this Reverse method is Void.

Now run the application and you will get the output as expected as shown below.

System.Collections.Generic Reverse Method in C#

How to apply Linq Reverse method on a collection of List<T> type?

If you want to apply the Reverse method which belongs to System.Linq namespace on a collection of type List<T>, then first you need to convert to the List<T> collection to as IEnumerable or IQueryable collection by using the AsEnumerable() or AsQueryable() method on the data source.

If you use the AsEnumerable() method then it will convert the collection to IEnumerable whereas if you use AsQueryable() method then it will convert the collection to IQueryable.

The following program shows how to apply the Linq Reverse method on a collection of type List<T>.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> stringList = new List<string>() { "Preety", "Tiwary", "Agrawal", "Priyanka", "Dewangan" };

            Console.WriteLine("Before Reverse the Data");
            foreach (var name in stringList)
            {
                Console.Write(name + " ");
            }
            Console.WriteLine();

            IEnumerable<string> ReverseData1 = stringList.AsEnumerable().Reverse();
            IQueryable<string> ReverseData2 = stringList.AsQueryable().Reverse();

            Console.WriteLine("After Reverse the Data");
            foreach (var name in ReverseData1)
            {
                Console.Write(name + " ");
            }

            Console.ReadKey();
        }
    }
}

Output:

Linq Reverse Method

Note: The LINQ Reverse Method in C# will return the data as IEnumerable<TSource> or IQuereable<TSource> based on how we use the LINQ the method.

In the next article, I am going to discuss the Linq Aggregate Methods in C# with examples. I hope this article gives you a very good understanding of how to use the Reverse Method in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *