开发者

Other Ways To Code Side By Side Enumeration?

开发者 https://www.devze.com 2023-02-27 15:29 出处:网络
I was wondering if any one could show me any other ways this method could be written, perhaps using LINQ?

I was wondering if any one could show me any other ways this method could be written, perhaps using LINQ?

private static bool CompareManyFoos(ManyFoos expected, ManyFoos actual)
{
    IEnumerator<Foo> expFooAtor = expected.GetEnumerator();
    IEnumerator<Foo> actFooAtor = actual.GetEnumerator();

    while (expFooAtor.MoveNext())
    {
        if (actFooAtor.MoveNext())
        {
            if (!FoosEqual(expFooAtor.Current, actFooAtor.Current)) return false;
        }
        else
        {
            MissingFoo(expFooAtor.Current);
            return false;
        }
    }
    return true;
}

EDIT

I've had to patch up my sample code a bit as I made some mistakes, sorry all. This is the original method, I adapted my sample code from:

    private static bool CompareXElementsChildXNodes(XElement expectedXElement, XElement actualXElement,
                                                    ref string message)
    {
        _itemLocator.LevelDown();

        IEnumerator<XNode> expectedNodeRator = expectedXEle开发者_JAVA百科ment.Nodes().GetEnumerator();
        IEnumerator<XNode> actualNodeRator = actualXElement.Nodes().GetEnumerator();

        while (expectedNodeRator.MoveNext())
        {
            if (actualNodeRator.MoveNext())
            {
                if (CompareXNodes(expectedNodeRator.Current, actualNodeRator.Current, ref message))
                {
                    _itemLocator.NextNode();
                }
                else
                {
                    return false;
                }
            }
            else
            {
                ExpectedXNodeActuallyMissing(expectedNodeRator.Current, ref message);
                return false;
            }
        }

        _itemLocator.LevelUp();

        return true;
    }


Perhaps SequenceEqual would be what you're looking for?

0

精彩评论

暂无评论...
验证码 换一张
取 消