For example let's say I have the following:
public class StringTester
{
IEnumerable<IStringConditional> Conditionals { get; }
public StringTester(IEnumerable<IStringConditional> conditionals)
{
conditionals = Conditionals;
}
public bool TestString(string testString)
{
foreach (var conditional in Conditionals)
{
if (!conditional.Test(testString))
{
return false;
}
}
return true;
}
}
Is this regarded as safe, or are IEnumerable types only safe for a single enumeration? If not what type would be best to use in a case like this?
It is undefined as to whether IEnumerable<T>
is repeatable in the general case. Often (usually): it is. However, examples exist that either:
So: in general I wouldn't advise ever iterating it more than once unless you know the scenario is a repeatable one. Things like arrays and lists are repeatable (if the data changes, of course, they may change to show those changes).