LINQ - prodot/ReCommended-Extension GitHub Wiki
Suggests replacing expressions with equivalent expressions that are more readable, or use modern language features, or improve performance. The analyzer doesn't make any suggestions if the current language version doesn't support the replacement.
TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index)
TSource ElementAt<TSource>(this IEnumerable<TSource> source, Index index)
If source
is an indexable** collection or a string:
Case | Suggested replacement | When suggested |
---|---|---|
source.ElementAt(index) |
source[index] |
always |
If source
is a distinct*** collection:
Case | Warns |
---|---|
source.ElementAt(index) |
that items should not be accessed by an index |
TSource? ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, int index)
TSource? ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, Index index)
If source
is a distinct*** collection:
Case | Warns |
---|---|
source.ElementAt(index) |
that items should not be accessed by an index |
TSource First<TSource>(this IEnumerable<TSource> source)
If source
is an indexable** collection or a string:
Case | Suggested replacement | When suggested |
---|---|---|
source.First() |
source[0] |
always |
If source
is a distinct*** collection:
Case | Warns |
---|---|
source.First() |
that items should not be accessed by an index |
TSource? FirstOrDefault<TSource>(this IEnumerable<TSource> source)
TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, TSource defaultValue)
If source
is an indexable** collection or a string:
Case | Suggested replacement | When suggested |
---|---|---|
source.FirstOrDefault() |
source is [var first, ..] ? first : default |
source is not null here |
source.FirstOrDefault(defaultValue) |
source is [var first, ..] ? first : defaultValue |
source is not null here |
If source
is a distinct*** collection:
Case | Warns |
---|---|
source.FirstOrDefault() |
that items should not be accessed by an index |
source.FirstOrDefault(defaultValue) |
that items should not be accessed by an index |
TSource Last<TSource>(this IEnumerable<TSource> source)
If source
is an indexable** collection or a string:
Case | Suggested replacement | When suggested |
---|---|---|
source.Last() |
source[^1] |
always |
If source
is a distinct*** collection:
Case | Warns |
---|---|
source.Last() |
that items should not be accessed by an index |
TSource? LastOrDefault<TSource>(this IEnumerable<TSource> source)
TSource LastOrDefault<TSource>(this IEnumerable<TSource> source, TSource defaultValue)
If source
is an indexable** collection or a string:
Case | Suggested replacement | When suggested |
---|---|---|
source.LastOrDefault() |
source is [.., var last] ? last : default |
source is not null here |
source.LastOrDefault(defaultValue) |
source is [.., var last] ? last : defaultValue |
source is not null here |
If source
is a distinct*** collection:
Case | Warns |
---|---|
source.LastOrDefault() |
that items should not be accessed by an index |
source.LastOrDefault(defaultValue) |
that items should not be accessed by an index |
long LongCount<TSource>(this IEnumerable<TSource> source)
Case | Suggested replacement | When suggested |
---|---|---|
source.LongCount() |
source.Length |
source is a string or an array |
source.LongCount() |
source.Count |
source is a collection* |
TSource Single<TSource>(this IEnumerable<TSource> source)
If source
is an indexable** collection or a string:
Case | Suggested replacement | When suggested |
---|---|---|
source.Single() |
source is [var item] ? item : throw new InvalidOperationException("... either empty or contains more than one element") |
source is not null here |
TSource? SingleOrDefault<TSource>(this IEnumerable<TSource> source)
TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, TSource defaultValue)
If source
is an indexable** collection or a string:
Case | Suggested replacement | When suggested |
---|---|---|
source.SingleOrDefault() |
source switch { [] => default, [var item] => item, _ => throw new InvalidOperationException("... contains more than one element") } |
source is not null here |
* Collections, which implement Collection<T>
or IReadOnlyCollection<T>
(including single-dimensional arrays), or strings.
**Indexable collections are collections, which implement IList<T>
(including single-dimensional arrays) or IReadOnlyList<T>
.
***Distinct collections are non-indexable collections, which implement ISet<T>
or IReadOnlySet<T>
, or the Dictionary<K,V>.KeyCollection
.
💡 Quick-fixes are available.
- list patterns might introduce variable with conflicting names
💡 The analyzers can be deactivated in the ReSharper Options dialog.