IEnumerable(T) assigned from constructor argument could be materialized


RH1005 Info

Cause

A System.Collections.Generic.IEnumerable<T> is used as a constructor argument and assigned within the constructor body to a field or property directly, without materialization.

Reason for rule

This rule does not identify code smells or susipicious sites directly. It serves to find sites that exhibit the behaviour described above, so that they can be manually reviewed.

An example scenario where materialization can reasonably be assumed to happen would be System.Collections.Generics.List<T> constructor accepting IEnumerable<T>. Or via counterexample, it would be highly unintuitive if mutations of the list would always iterate over the passed in IEnumerable<T>.

How to fix violations

Materialize the passed in IEnumerable<T> in the constructor body, e.g. with ToList().

Examples

Violates

class MyClass
{
	public IEnumerable<int> Values { get; }	

	public MyClass(IEnumerable<int> values)
	{
		Values = values;
	}
}

Does not violate

class MyClass
{
	public IEnumerable<int> Values { get; }	

	public MyClass(IEnumerable<int> values)
	{
		Values = values.ToList();
	}
}