Gendarme.Rules.Performance.ReviewLinqMethodRule Class
Linq extension methods operate on sequences of values so they generally have linear time complexity. However you may be able to achieve better than linear time performance if you use a less general method or take advantage of a method provided by an Sytem.Collections.Generic.IEnumerable<T> subclass.

See Also: ReviewLinqMethodRule Members

Syntax

[Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine, Gendarme.Framework, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null))]
[Gendarme.Framework.Problem("A linq extension method with linear time complexity is used, but a more efficient method is available.")]
[Gendarme.Framework.Solution("Use the more efficient method.")]
public sealed class ReviewLinqMethodRule : Gendarme.Framework.Rule, Gendarme.Framework.IMethodRule

Remarks

This rule is available since Gendarme 2.6

Example

Bad example:

Example

            public string FirstOrMissing (IEnumerable<string> sequence, string missing)
            {
            	// Count () is O(n)
            	if (sequence.Count () > 0) {
            		return sequence.First ();
            	}
            	return missing;
            }
            public void Append (List<string> lines, string line)
            {
            	// Last () is O(n)
            	if (lines.Count == 0 || lines.Last () != line) {
            		lines.Add (line);
            	}
            }
            

Example

Good example:

Example

            public string FirstOrMissing (IEnumerable<string> sequence, string missing)
            {
            	// We don't need an exact count so we can use the O(1) Any () method.
            	if (sequence.Any ()) {
            		return sequence.First ();
            	}
            	return missing;
            }
            public void Append (List<string> lines, string line)
            {
            	// Lines is a List so we can use the O(1) subscript operator instead of
            	// the Last () method.
            	if (lines.Count == 0 || lines [lines.Count - 1] != line) {
            		lines.Add (line);
            	}
            }
            

Requirements

Namespace: Gendarme.Rules.Performance
Assembly: Gendarme.Rules.Performance (in Gendarme.Rules.Performance.dll)
Assembly Versions: 2.8.0.0