C# Program to find the longest string from an array of strings using Lambda Expression

Lambda expressions in C# provide a concise way to write anonymous functions. When working with arrays of strings, you can use lambda expressions with LINQ methods like Aggregate() to perform operations such as finding the longest string.

In this example, we'll demonstrate how to find the longest string from an array using the Aggregate() method with a lambda expression.

Syntax

The Aggregate() method with lambda expression follows this syntax −

collection.Aggregate(seed, (accumulator, next) => condition ? next : accumulator, result => transformation)

Where −

  • seed − Initial value for comparison
  • accumulator − Current result being built
  • next − Next element in the collection
  • result − Final transformation applied to the result

Using Aggregate() with Seed Value

The following example uses a seed value to ensure the result has more characters than the initial comparison string −

using System;
using System.Linq;

class Demo {
    static void Main() {
        string[] arr = { "Java", "HTML", "CSS", "JavaScript"};
        string res = arr.AsQueryable().Aggregate("jQuery", (longest, next) => next.Length > longest.Length ? next : longest, str => str.ToLower());
        Console.WriteLine("String with more number of characters: {0}", res);
    }
}

The output of the above code is −

String with more number of characters: javascript

Finding Longest String Without Seed Value

You can also find the longest string directly from the array without using a seed value −

using System;
using System.Linq;

class Demo {
    static void Main() {
        string[] arr = { "Java", "HTML", "CSS", "JavaScript", "C#"};
        string longest = arr.Aggregate((str1, str2) => str1.Length > str2.Length ? str1 : str2);
        Console.WriteLine("Longest string: {0}", longest);
        Console.WriteLine("Length: {0}", longest.Length);
    }
}

The output of the above code is −

Longest string: JavaScript
Length: 10

Using OrderBy with Lambda Expression

An alternative approach using OrderBy() with lambda expression −

using System;
using System.Linq;

class Demo {
    static void Main() {
        string[] arr = { "Java", "HTML", "CSS", "JavaScript", "Python", "Go"};
        string longest = arr.OrderBy(s => s.Length).Last();
        Console.WriteLine("Longest string using OrderBy: {0}", longest);
        
        // Get all strings with maximum length
        var maxLength = arr.Max(s => s.Length);
        var longestStrings = arr.Where(s => s.Length == maxLength);
        Console.WriteLine("All longest strings: {0}", string.Join(", ", longestStrings));
    }
}

The output of the above code is −

Longest string using OrderBy: JavaScript
All longest strings: JavaScript

Comparison of Approaches

Method Performance Use Case
Aggregate() without seed O(n) Simple longest string finding
Aggregate() with seed O(n) When you need minimum length comparison
OrderBy().Last() O(n log n) When you need sorted results

Conclusion

Lambda expressions with LINQ methods like Aggregate() provide an elegant way to find the longest string in an array. The Aggregate() method is most efficient for this task, while OrderBy() offers additional flexibility when you need multiple results or sorted data.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements