Skip to content

Files

Latest commit

 

History

History
43 lines (31 loc) · 991 Bytes

File metadata and controls

43 lines (31 loc) · 991 Bytes

Pattern: Use of Enumerable LINQ method

Issue: -

Description

The Enumerable LINQ method was used on a type that supports an equivalent, more efficient property.

This rule flags calls to the following methods on these collection types:

  • System.Linq.Enumerable.Count
  • System.Linq.Enumerable.First
  • System.Linq.Enumerable.FirstOrDefault
  • System.Linq.Enumerable.Last
  • System.Linq.Enumerable.LastOrDefault

The analyzed collection types and methods may be extended in the future to cover more cases.

Example of incorrect code:

public void Test(IReadOnlyList<string> list)
{
	Console.Write(list.First());
	Console.Write(list.Last());
	Console.Write(list.Count());
}

Example of correct code:

public void Test(IReadOnlyList<string> list)
{
	Console.Write(list[0]);
	Console.Write(list[list.Count - 1]);
	Console.Write(list.Count);
}

Further Reading