C# Program to invert the order of elements in a sequence

CsharpProgrammingServer Side Programming

Set a string.

char[] ch = { 'd', 'r', 'i', 'v', 'e' };
Console.Write("String =
foreach(char arr in ch)
Console.Write(arr);

Now, use Queryable Reverse() method to invert the order of elements.

IQueryable<char> res = ch.AsQueryable().Reverse();

Let us see the complete code.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      char[] ch = { 'd', 'r', 'i', 'v', 'e' };
      Console.Write("String = ");
      foreach(char arr in ch)
      Console.Write(arr);
      IQueryable<char> res = ch.AsQueryable().Reverse();
      Console.Write("\nReversed = ");
      foreach (char c in res)
      Console.Write(c);
   }
}

Output

String = drive
Reversed = evird
raja
Published on 12-Sep-2018 09:19:38
Advertisements