/
Enumerable.cs
82 lines (80 loc) · 2.1 KB
/
Enumerable.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
namespace System.Linq
{
/// <summary>
/// Re-implements parts of System.Core.dll for use with .NET 2.0
/// </summary>
public static class Enumerable
{
/// <summary>
/// Returns the last element of a sequence.
/// </summary>
///
/// <typeparam name="TSource">
/// The type of the elements of <paramref name="source"/>.
/// </typeparam>
///
/// <param name="source">
/// An <see cref="IEnumerable{T}"/> to return the last element of.
/// </param>
///
/// <returns>
/// The value at the last position in the source sequence.
/// </returns>
///
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
///
/// <exception cref="InvalidOperationException">
/// The source sequence is empty.
/// </exception>
public static TSource Last<TSource>(this IEnumerable<TSource> source)
{
if (null == source)
{
throw new ArgumentNullException("source");
}
var e = source.GetEnumerator();
if(!e.MoveNext())
{
throw new InvalidOperationException();
}
TSource item = e.Current;
while (e.MoveNext())
{
item = e.Current;
}
return item;
}
/// <summary>
/// Determines whether a sequence contains any elements.
/// </summary>
///
/// <typeparam name="TSource">
/// The type of the elements of <paramref name="source"/>.
/// </typeparam>
///
/// <param name="source">
/// An <see cref="IEnumerable{T}"/> to check for emptiness.
/// </param>
///
/// <returns>
/// <see langword="true"/> if the source sequence contains any elements; otherwise <see langword="false"/>.
/// </returns>
///
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
public static bool Any<TSource>(this IEnumerable<TSource> source)
{
if (null == source)
{
throw new ArgumentNullException("source");
}
var e = source.GetEnumerator();
return e.MoveNext();
}
}
}