Is there a circular reference in a set of template substitutions?
If you convert your dictionary into directed graph, you should be able to do a topological sorting. Kahn's algorithm works fairly well, and is easy to implement, especially if you drop all the stuff related to actual sorting. 

static void ValidateIsNotCircular(Dictionary&lt;string, IEnumerable&lt;string&gt;&gt; dict)
{
    var edges = dict.SelectMany(x =&gt; x.Value.Select(y =&gt; Tuple.Create(x.Key, y))).ToList();
    var topLevelNodes = new Stack&lt;string&gt;(dict.Keys.Where(n =&gt; !edges.Any(e =&gt; e.Item2.Equals(n))));

    while (topLevelNodes.Any())
    {
        var node = topLevelNodes.Pop();
        var outgoingEdges = edges.Where(e =&gt; e.Item1.Equals(node)).ToArray();
        foreach(var edge in outgoingEdges)
        {
            edges.Remove(edge);
            var childNode = edge.Item2;
            var childHasIncomingEdges = edges.Any(e =&gt; e.Item2.Equals(childNode));
            if (!childHasIncomingEdges)
            {
                topLevelNodes.Push(childNode);
            }
        }
    }

    if (edges.Any()) 
    {
        throw new CircularPathException(edges.Select(e =&gt; e.Item1).Distinct());
    } 
}


This way you don't have to deal with iterators.