-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
ElementAt-Sample1-.cs
37 lines (31 loc) · 1.2 KB
/
ElementAt-Sample1-.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Linq;
namespace Element
{
public static class ElementAtSample1
{
// This sample uses ElementAt and query syntaxto retrieve the second number greater than 5 from an array.
// Output:
// Second number > 5: 8
public static void QuerySyntaxExample()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int fourthLowNum = (
from num in numbers
where num > 5
select num)
.ElementAt(1); // second number is index 1 because sequences use 0-based indexing
Console.WriteLine("Second number > 5: {0}", fourthLowNum);
}
// This sample uses ElementAt and method syntax to retrieve the second number greater than 5 from an array.
// Output:
// Second number > 5: 8
public static void MethodSyntaxExample()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// Second number is index 1 because sequences use 0-based indexing.
int fourthLowNum = (numbers.Where(num => num > 5)).ElementAt(1);
Console.WriteLine($"Second number > 5: {fourthLowNum}");
}
}
}