C# 中断返回列表c的函数#

C# 中断返回列表c的函数#,c#,.net,winforms,C#,.net,Winforms,我有一个函数,它返回treenodes的列表。我想退出for循环,并在以下条件未满足时退出函数并停止运行程序。 我知道在这种情况下,return可以正常工作,但在第一个return消息中有一个错误 需要可转换为“System.Collections.Generic.List”类型的对象 我的职能: public List<TreeNode> FindIndex(TreeNodeCollection nodes, List<TreeNode> list) { int

我有一个函数,它返回
treenodes
的列表。我想退出
for
循环,并在以下
条件未满足时退出函数并停止运行程序。
我知道在这种情况下,
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;
}
公共列表FindIndex(TreeNodeCollection节点,列表)
{
int-idx;
foreach(节点中的树节点)
{
如果(Node.Text==“测试”)
{
idx=节点索引;
列表。添加(节点);
}
其他的
{
bool isEmpty=!list.Any();
如果(我是空的)
{
Show(“此XML文件不包含任何名为“test\”!)的节点”;
回来
}
}
FindIndex(Node.Nodes,列表);
}
退货清单;
}

我尝试了
中断
,但它不会停止程序,只会中断不运行整个代码的函数。

如果你想中断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;
}
公共列表FindIndex(TreeNodeCollection节点,列表)
{
int-idx;
foreach(节点中的树节点)
{
如果(Node.Text==“测试”)
{
idx=节点索引;
列表。添加(节点);
}
其他的
{
Show(“此XML文件不包含任何名为“test\”!)的节点”;
退货清单;
}
FindIndex(Node.Nodes,列表);
}
退货清单;
}

如果要中断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;
}
公共列表FindIndex(TreeNodeCollection节点,列表)
{
int-idx;
foreach(节点中的树节点)
{
如果(Node.Text==“测试”)
{
idx=节点索引;
列表。添加(节点);
}
其他的
{
Show(“此XML文件不包含任何名为“test\”!)的节点”;
退货清单;
}
FindIndex(Node.Nodes,列表);
}
退货清单;
}

您可以使用
中断
退出
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;
}
公共列表FindIndex(TreeNodeCollection节点,列表){
int-idx;
foreach(节点中的树节点)
{
如果(Node.Text==“测试”)
{
idx=节点索引;
列表。添加(节点);
}
其他的
{
Show(“此XML文件不包含任何名为“test\”!)的节点”;
打破
}
FindIndex(Node.Nodes,列表);
}
退货清单;
}

您可以使用
中断
退出
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;
}
公共列表FindIndex(TreeNodeCollection节点,列表){
int-idx;
foreach(节点中的树节点)
{
如果(Node.Text==“测试”)
{
idx=节点索引;
列表。添加(节点);
}
其他的
{
Show(“此XML文件不包含任何名为“test\”!)的节点”;
打破
}
FindIndex(Node.Nodes,列表);
}
退货清单;
}

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

  // 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();
//您要查找的是节点,不是索引,对吗?
//要返回节点还是附加列表?让我们返回节点
公共IEnumerable FindNodes(TreeNodeCollection节点){
if(null==节点)
抛出新的ArgumentNullException(“节点”);
//假设模式不包含空节点
foreach(节点中的树节点)
如果(node.Text==“测试”)
收益回报节点;
否则{
//您的测试消息,请在发布中注释掉它
Show(“此XML文件不包含任何名为“test\”!)的节点”;
产量中断;//没有更多项目
}
}
...
List=FindNodes(myNodes).ToList();
从技术上讲,该方法本身是一种超调,您可以只放置一个简单易读的Linq查询:

List=节点
第()类
.TakeWhile(node=>node.Text==“测试”)
.ToList();

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

  // 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();
//您要查找的是节点,不是索引,对吗?
//要返回节点还是附加列表?让我们返回节点
公共IEnumerable FindNodes(TreeNodeCollection节点){
if(null==节点)
抛出新的ArgumentNullException(“节点”);
//假设模式不包含空节点
foreach(节点中的树节点)
如果(node.Text==“测试”)
收益回报节点;
否则{
//您的测试消息,请在发布中注释掉它
Show(“此XML文件不包含任何名为“test\”!)的节点”;
产量中断;//没有更多项目
}
}
...
List=FindNodes(myNodes).ToList();
从技术上讲,该方法本身是一种超调,您可以只放置一个简单易读的Linq查询:

List=节点
第()类
.TakeWhile(node=>node.Text==“测试”)
.ToList();

您可以这样退出应用程序

   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;
    }
公共列表FindIndex(TreeNodeCollection节点,列表){
int-idx;
foreach(节点中的树节点)
{
如果(Node.Text==“测试”)
{
idx=节点索引;
列表。添加(节点);
}
其他的
{
Show(“此XML文件不包含任何名为“test\”!)的节点”;
Application.Exit();
}
FindIndex(Node.Nodes,列表);
}
退货清单;
}

上面的代码将中断循环,退出函数,甚至停止程序。我必须说,退出程序不是一种优雅的方式

   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;
    }
公共列表FindIndex(TreeNodeCollection节点,列表){
int-idx;
foreach(节点中的树节点)
{
如果(Node.Text==“测试”)
{
idx=节点索引;
列表。添加(节点);
}
其他的
{
Show(“此XML文件不包含任何名为