Aggregate string value

In this chapter you will learn:

  1. Aggregate two string values
  2. Use Aggregate the reverse a string

Aggregate two string values

using System;/*j  a  va2  s.  c  o m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string[] curries = { "abc", "abcd", "ab" };
        Console.WriteLine(curries.Aggregate((a, b) => a + " " + b));
    }
}

Aggregate to reverse a string

using System;/* j  a  v a 2 s . c  om*/
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);
    }        
}

Next chapter...

What you will learn in the next chapter:

  1. How to use All operator
  2. All with condition
  3. Using All to determine whether an array contains only odd numbers
Home » C# Tutorial » Linq Operators
Aggregate
Aggregate with seed
Aggregate string value
All
Any
Average
Cast
Concat
Contains
Count
DefaultIfEmpty
Distinct
ElementAt
ElementAtOrDefault
Empty
Except
FindAll
First
FirstOrDefault
GroupBy
Intersect
Last
LastOrDefault
LongCount
Max
Min
OfType
OrderBy
OrderByDescending
Range
Repeat
Reverse
SelectMany
SequenceEqual
Single
SingleOrDefault
Skip
SkipWhile
Sum
Take
TakeWhile
ThenBy
ThenByDescending
ToArray
ToList
Zip