Use Max to get longest word in a string array by comparing length of the string in CSharp

Description

The following code shows how to use Max to get longest word in a string array by comparing length of the string.

Example


  /*from   w  ww.  ja  va2  s  .  c  o m*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {
        string[] words = { "cherry", "apple", "blueberry" };

        int longestLength = words.Max(w => w.Length);

        Console.WriteLine("The longest word is {0} characters long.", longestLength);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ