Mega Code Archive

 
Categories / C# Tutorial / LINQ
 

Use Linq Aggregate the reverse a string

using System; using System.Collections.Generic; using System.Linq;     class Program     {         private static string ReverseString(string sentence)         {             List<string> words = new List<string>(sentence.Split(' ').ToList());             return words.Aggregate((a, b) => b + " " + a);                     }         static void Main(string[] args)         {             string sentence = "this is a test";             string result = ReverseString(sentence);             Console.WriteLine(result);         }             }