diff options
author | Marek Safar <marek.safar@gmail.com> | 2008-07-24 14:01:27 +0400 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2008-07-24 14:01:27 +0400 |
commit | c3db302c0b39b35110cbd760d0ca6f564ac2517b (patch) | |
tree | 7176cffba0e1f457807dafe0f59b115f416a3f6e /mcs/class/System.Core/System.Linq/Enumerable.cs | |
parent | c929cbbde8f573711d650f5d70a228829e94f1f8 (diff) |
2008-07-24 Marek Safar <marek.safar@gmail.com>
* Enumerable.cs (First): Optimized.
svn path=/trunk/mcs/; revision=108640
Diffstat (limited to 'mcs/class/System.Core/System.Linq/Enumerable.cs')
-rw-r--r-- | mcs/class/System.Core/System.Linq/Enumerable.cs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/mcs/class/System.Core/System.Linq/Enumerable.cs b/mcs/class/System.Core/System.Linq/Enumerable.cs index 3ac3fc5a4a2..b7829d77a73 100644 --- a/mcs/class/System.Core/System.Linq/Enumerable.cs +++ b/mcs/class/System.Core/System.Linq/Enumerable.cs @@ -578,8 +578,21 @@ namespace System.Linq public static TSource First<TSource> (this IEnumerable<TSource> source) { Check.Source (source); - - return source.First (PredicateOf<TSource>.Always, Fallback.Throw); + + var list = source as IList<TSource>; + if (list != null) { + if (list.Count != 0) + return list [0]; + + throw new InvalidOperationException (); + } else { + using (var enumerator = source.GetEnumerator ()) { + if (enumerator.MoveNext ()) + return enumerator.Current; + } + } + + throw new InvalidOperationException (); } public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate) |