Last
In this chapter you will learn:
Get to know Last operator
using System;/*java 2 s . c om*/
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);
}
}
Last operator with string operation
using System;/* j a v a2s . c o m*/
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(p => p.StartsWith("H"));
Console.WriteLine(name);
}
}
Last operator with filter delegate
using System;// java2s . co m
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
int last = numbers.Last(); // 5
int lastEven = numbers.Last(n => n % 2 == 0); // 4
Console.WriteLine(last);
Console.WriteLine(lastEven);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » Linq Operators