using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class Employee { public int birthYear; public string firstName; public string lastName; public static Employee[] GetEmployees() { Employee[] actors = new Employee[] { new Employee { birthYear = 1964, firstName = "K", lastName = "R" }, new Employee { birthYear = 1968, firstName = "O", lastName = "W" }, new Employee { birthYear = 1960, firstName = "J", lastName = "S" }, new Employee { birthYear = 1964, firstName = "S", lastName = "B" }, }; return (actors); } } public class MainClass { public static void Main() { int youngestEmployeeAge = Employee.GetEmployees().Max(a => a.birthYear); Console.WriteLine(youngestEmployeeAge); } }
LINQ
Max on String array
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { string[] presidents = {"G", "H", "H", "H", "H", "J"}; string maxName = presidents.Max(); Console.WriteLine(maxName); } }
Max on int array
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { int[] myInts = new int[] { 974, 2, 7, 1374, 27, 54 }; int maxInt = myInts.Max(); Console.WriteLine(maxInt); } }
LongCount with &&(and) operator
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
long count = Enumerable.Range(0, int.MaxValue).
Concat(Enumerable.Range(0, int.MaxValue)).LongCount(n => n > 1 && n < 4);
Console.WriteLine(count);
}
}
[/csharp]
LongCount Prototype
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { long count = Enumerable.Range(0, int.MaxValue).Concat(Enumerable.Range(0, int.MaxValue)).LongCount(); Console.WriteLine(count); } }
LastOrDefault with string operator
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; string name = presidents.LastOrDefault(p => p.StartsWith("Z")); Console.WriteLine(name == null ? "NULL" : name); } }
LastOrDefault returns null when an Element Is Found
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; string name = presidents.LastOrDefault(); Console.WriteLine(name == null ? "NULL" : name); } }