static UFD searchfather(string fathername,UFD rootnode)
        {//递归地寻找以home为根的name为fathername的节点
            //如何打断for()递归
            if (rootnode.name == fathername && rootnode.childUFD.Count < rootnode.childUFDcount)// && isGo==true)
            {
                return rootnode;
            }
            else
            {
                for (int i = 0; i < rootnode.childUFDcount; i++)
                {
                    searchfather(fathername, (rootnode.childUFD[i]) as UFD);
                }
            }
            return null;
        }找到节点并返回后,程序还会继续执行。请问该怎么改或者其他什么好的算法。

解决方案 »

  1.   

    用yield return/yield break实现迭代器。
      

  2.   

    MSDN上搜搜 break 和continue 关键字
      

  3.   

    http://www.soaspx.com/dotnet/csharp/csharp_20120613_9276.html
      

  4.   

    for (int i = 0; i < rootnode.childUFDcount; i++)
                    {
                        var ufd=searchfather(fathername, (rootnode.childUFD[i]) as UFD);
                            if(ufd != null)
                            {
                                return ufd;
                            }
                    }
      

  5.   

    static int a=0;
    static UFD searchfather(string fathername,UFD rootnode)
            {//递归地寻找以home为根的name为fathername的节点
                //如何打断for()递归
                if(a==1)
    {return null;//对空判断自行处理}
                if (rootnode.name == fathername && rootnode.childUFD.Count < rootnode.childUFDcount)// && isGo==true)
                {
                a=1;                return rootnode;
                }
                else
                {
                    for (int i = 0; i < rootnode.childUFDcount; i++)
                    {
                        searchfather(fathername, (rootnode.childUFD[i]) as UFD);
                    }
                }
                return null;
            }
      

  6.   

    IEnumerable不能隐形转换,用了6楼的例子,IEnumerable<UFD>居然无法识别我的类,不知道怎么回事
      

  7.   

    者是递归了你还用FOR,这个问题的关键在于要明白什么时候跳出FOR,