C# - Reverse the list of cities using LINQ

Learn, how to reverse the list of cities using Linq in C#? By Nidhi Last updated : April 01, 2023

Here, we will create a list of cities. Then reverse the list of cities using the Linq Reverse() method and print them on the console screen.

C# program to reverse the list of cities using LINQ

The source code to reverse the list of cities using Linq is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - Reverse the list of cities using LINQ.

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

class Program
{
    static void Main(string[] args)
    {
        List<string> citys = new List<string>() {"Delhi","Ahmadabad","Agra","Mumbai"};

        citys.Reverse();

        Console.WriteLine("Reversed list of cities:");
        foreach (var item in citys)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();
    }
}

Output

Reversed list of cities:
Mumbai Agra Ahmadabad Delhi
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 the list of cities and then reverse the list 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.