<p>I'm trying to create an algorithm in C# which produces the following output strings:</p>

<pre><code>AAAA
AAAB
AAAC
...and so on...
ZZZX
ZZZY
ZZZZ
</code></pre>

<p>What is the best way to accomplish this?</p>

<pre><code>public static IEnumerable&lt;string&gt; GetWords()
{
    //Perform algorithm
    yield return word;
}
</code></pre>

well, if the length is a constant 4, then this would handle it:
```
```
public static IEnumerable<String> GetWords()
{
    for (Char c1 = 'A'; c1 <= 'Z'; c1++)
    {
        for (Char c2 = 'A'; c2 <= 'Z'; c2++)
        {
            for (Char c3 = 'A'; c3 <= 'Z'; c3++)
            {
                for (Char c4 = 'A'; c4 <= 'Z'; c4++)
                {
                    yield return "" + c1 + c2 + c3 + c4;
                }
            }
        }
    }
}
```
```
if the length is a parameter, this recursive solution would handle it:
```
```
public static IEnumerable<String> GetWords(Int32 length)
{
    if (length <= 0)
        yield break;

    for (Char c = 'A'; c <= 'Z'; c++)
    {
        if (length > 1)
        {
            foreach (String restWord in GetWords(length - 1))
                yield return c + restWord;
        }
        else
            yield return "" + c;
    }
}
```
```