How to generate Variations with repetition interatively in C#

// (red, red)
// (red, green)
// (red, blue)
// (green, red)
// (green, green)
// (green, blue)
// (blue, red)
// (blue, green)
// (blue, blue)

using System;
using System.Linq;

public static class Program
{
    private static readonly string[] Fruits = { "red", "green", "blue"};

    public static void Main()
    {
        var k = 2;
        var n = 3;
        var arr = new int[k];

        while (true)
        {
            Console.WriteLine($"({string.Join(", ", arr.Select(e => Fruits[e]))})");

            var index = k - 1;
            while (index >= 0 && arr[index] == n - 1)
            {
                index--;
            }

            if (index < 0)
            {
                break;
            }

            arr[index]++;

            for (int i = index + 1; i < k; i++)
            {
                arr[i] = 0;
            }
        }
    }
}

How to generate Variations without repetition recursively in C#

using System;
using System.Linq;

class VariationsWithoutRepetition
{
    const int k = 2;
    const int n = 4;

    static int[] arr = new int[k];
    static int[] free = Enumerable.Range(1, 4).ToArray();

    static void Main()
    {
        GenerateVariationsNoRepetitions(0);
    }

    static void GenerateVariationsNoRepetitions(int index)
    {
        if (index >= k)
        {
            PrintVariations();
        }
        else
        {
            for (int i = index; i < n; i++)
            {
                arr[index] = free[i];
                Swap(ref free[i], ref free[index]);
                GenerateVariationsNoRepetitions(index + 1);
                Swap(ref free[i], ref free[index]);
            }
        }
    }

    private static void Swap<T>(ref T v1, ref T v2)
    {
        T old = v1;
        v1 = v2;
        v2 = old;
    }

    static void PrintVariations()
    {
        Console.WriteLine("(" + string.Join(", ", arr) + ")");
    }
}


// result:
// (1, 2)
// (1, 3)
// (1, 4)
// (2, 1)
// (2, 3)
// (2, 4)
// (3, 2)
// (3, 1)
// (3, 4)
// (4, 2)
// (4, 3)
// (4, 1)