using System.Collections.Generic;
public static class Program
public static readonly int N = 5;
public static void Main()
List<int> max = new List<int>() {3,4};
RecursiveForeach(max, 0, Enumerable.Repeat(0, max.Count).ToList());
public static void RecursiveForeach(List<int> maxValues, int depth, List<int> currentValues)
if (depth == maxValues.Count)
if (currentValues.Sum()<=N)
Console.WriteLine(string.Join(",",currentValues));
for (int val = 0; val <= maxValues[depth]; val++)
currentValues[depth] = val;
RecursiveForeach(maxValues, depth+1, currentValues);