破坏返回列表 c# 的函数

本文关键字:函数 列表 返回 | 更新日期: 2023-09-27 18:36:23

我有一个返回treenodes列表的函数。我想退出for循环并退出函数并在不满足以下if条件时停止运行程序。我知道在这种情况下return工作正常,但在这里我在消息的第一个return中出现了错误。

需要可转换为"System.Collections.Generic.List"类型的对象

我的职能:

public List<TreeNode> FindIndex(TreeNodeCollection nodes, List<TreeNode> list)
{
    int idx;
    foreach (TreeNode Node in nodes)
    {
        if (Node.Text == "test")
        {
            idx = Node.Index;
            list.Add(Node);
        }
        else
        {
            bool isEmpty = !list.Any();
            if (isEmpty)
            {
                MessageBox.Show("This XML file does not contain any node with name '"test'"!");
                return;
            }
        }
        FindIndex(Node.Nodes, list);
    }
    return list;
}

我已经尝试了break但它不会停止程序,只会破坏不运行整个代码的函数。

破坏返回列表 c# 的函数

我建议将解决方案重新设计为

  // You're looking for nodes, not indexes, right?
  // Do you want to return the nodes or append the list? Let's return nodes
  public IEnumerable<TreeNode> FindNodes(TreeNodeCollection nodes) {
    if (null == nodes)
      throw new ArgumentNullException("nodes");
    // providing that modes doesn't contain null nodes
    foreach (Treenode node in nodes)
      if (node.Text == "test")
        yield return node;
      else {
        // Your test message, comment out it in the release 
        MessageBox.Show("This XML file does not contain any node with name '"test'"!");
        yield break; // no more items 
      }
  }
...
  List<TreeNode> list = FindNodes(myNodes).ToList();

从技术上讲,该方法本身是一个超调,您可以只放置一个简单易读的 Linq 查询:

  List<TreeNode> list = nodes
    .OfType<TreeNode>()
    .TakeWhile(node => node.Text == "test")
    .ToList();
您可以使用

break;退出foreach

public List<TreeNode> FindIndex(TreeNodeCollection nodes, List<TreeNode> list) {
int idx;
foreach (TreeNode Node in nodes)
{
    if (Node.Text == "test")
    {
        idx = Node.Index;
        list.Add(Node);
    }
    else
    {
        MessageBox.Show("This XML file does not contain any node with name '"test'"!");
        break;
    }
    FindIndex(Node.Nodes, list);
}
return list;
}

如果你想打破for循环

public List<TreeNode> FindIndex(TreeNodeCollection nodes, List<TreeNode> list)
{
    int idx;
    foreach (TreeNode Node in nodes)
    {
        if (Node.Text == "test")
        {
            idx = Node.Index;
            list.Add(Node);
        }
        else
        {
            MessageBox.Show("This XML file does not contain any node with name '"test'"!");
            return list;
        }
        FindIndex(Node.Nodes, list);
    }
    return list;
}

你可以像这样退出应用程序

   public List<TreeNode> FindIndex(TreeNodeCollection nodes, List<TreeNode> list) {
    int idx;
    foreach (TreeNode Node in nodes)
    {
        if (Node.Text == "test")
        {
            idx = Node.Index;
            list.Add(Node);
        }
        else
        {
            MessageBox.Show("This XML file does not contain any node with name '"test'"!");
            Application.Exit();
        }
        FindIndex(Node.Nodes, list);
    }
    return list;
    }

上面的代码会打破循环,退出函数,甚至停止程序。我必须说不是退出程序的优雅方式

可以在代码中的任何位置调用该方法Application.Exit,以立即停止整个程序。这样,您就不必从函数返回并等待所有其他代码执行。

另请参阅 MSDN 文档:https://msdn.microsoft.com/en-us/library/ms157894(v=vs.110).aspx