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);
}
}
Author: coder
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);
}
}
Call LastOrDefault returned from Take
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.Take(0).LastOrDefault();
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);
}
}
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);
}
}
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);
}
}
A lambda expression has the following BNF form: (parameters) => expression-or-statement-block
using System;
class Test {
static void Main() {
Func<int, int> square = x => x * x;
Console.WriteLine(square(3)); // 9
}
}