.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		// Iterate from a starting point until you encounter any elements defined as 
		// terminating elements
		var terminatingElements = new HashSet<int>{ 7, 9, 11 };
		// This will iterate from 1 until one of the terminating  elements is encountered (7)
		foreach(var x in CountUntilAny(1,terminatingElements))
		{
			Console.WriteLine(x);
		}
	}
	
	public static IEnumerable<int> CountUntilAny(int start, HashSet<int> earlyTerminationSet)
	{
		int curr = start;
	
		while (true)
		{
			if (earlyTerminationSet.Contains(curr))
			{
				// we've hit one of the ending values
				yield break;
			}
	
			yield return curr;
	
			if (curr == Int32.MaxValue)
			{
				// don't overflow if we get all the way to the end; just stop
				yield break;
			}
	
			curr++;
		}
	}
}
;