You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Expressions/Collections/EnumerateOnce.cs

29 lines
547 B

2 years ago
using System.Collections;
namespace Connected.Expressions.Collections;
internal class EnumerateOnce<T> : IEnumerable<T>, IEnumerable
{
private IEnumerable<T>? _enumerable;
public EnumerateOnce(IEnumerable<T> enumerable)
{
_enumerable = enumerable;
}
public IEnumerator<T> GetEnumerator()
{
var en = Interlocked.Exchange(ref _enumerable, null);
if (en is not null)
return en.GetEnumerator();
throw new Exception("Enumerated more than once.");
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}