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);
}
}
Month: February 2011
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
}
}
A lambda expression can reference the local variables and parameters of the method in which it's defined.
using System;
delegate int NumericSequence();
class Test {
static void Main() {
int seed = 0;
NumericSequence natural = () => seed++;
Console.WriteLine(natural());
Console.WriteLine(natural());
}
}
Last Prototype
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.Last();
Console.WriteLine(name);
}
}

