C# - Example of LINQ Reverse() Method

Here, we are going to learn about the Linq Reverse() method and C# implementation.
By Nidhi Last updated : April 01, 2023

Here, we will create an array of integers. Then reverse the array using Linq Reverse() method and print the reversed array on the console screen.

C# program to demonstrate the example of LINQ Reverse() method

The source code to demonstrate the Linq Reverse() method, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate Linq Reverse() method.

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = new int[] { 20,50,40,39,43,23,45,17,38};
        
        IEnumerable<int> result = numbers.Reverse();

        Console.WriteLine("Reversed array:");
        foreach (var item in result)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();
    }
}

Output

Reversed array:
38 17 45 23 43 39 40 50 20
Press any key to continue . . .

Explanation

In the above program, we created a class Demo that contains the Main() method. The Main() method is the entry point for the program.

In the Main() method, we created an array of integers and then reverse the array using the Reverse() method and then print them using the "foreach" loop on the console screen.

C# LINQ Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.