Foreach 无法对方法组进行操作

发布于 2024-12-11 16:45:27 字数 229 浏览 5 评论 0原文

private void PrintRecursive(TreeNode treeNode)
    {
        foreach (TreeNode tn in treeNode.Nodes)
        {
            PrintRecursive(tn);
        }
    }

我收到错误:Foreach 无法对方法组进行操作。您打算调用“方法组”吗?

private void PrintRecursive(TreeNode treeNode)
    {
        foreach (TreeNode tn in treeNode.Nodes)
        {
            PrintRecursive(tn);
        }
    }

I get the error: Foreach cannot operate on a method group. Did you intend to invoke the 'method group'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心是晴朗的。 2024-12-18 16:45:27

这里的问题是 Nodes 是一个方法,但您用作属性:)
所以这行代码

foreach (TreeNode tn in treeNode.Nodes)

应该是

foreach (TreeNode tn in treeNode.Nodes())

The problem here is that Nodes is a method but you use as property :)
So this line of code

foreach (TreeNode tn in treeNode.Nodes)

should be

foreach (TreeNode tn in treeNode.Nodes())
枫林﹌晚霞¤ 2024-12-18 16:45:27

假设您使用的是打包的 TreeView 控件,它不应该是 ChildNodes 吗?:

foreach (TreeNode node in treeNode.ChildNodes) ...

Assuming you're using the packaged TreeView control, shouldn't it be ChildNodes?:

foreach (TreeNode node in treeNode.ChildNodes) ...
情痴 2024-12-18 16:45:27

TreeView.Nodes 给出TreeNode 对象的集合,表示 TreeView 控件中的根节点。

要访问根节点的子节点,请使用 ChildNodes 节点的属性。

例如使用 for 循环

void  PrintRecursive(TreeNode node)
{
  for(int i=0; i <node.ChildNodes.Count; i++)
  {
    PrintRecursive(node.ChildNodes[i]);
  }
}

或使用 foreach

void  PrintRecursive(TreeNode node)
{
  foreach(TreeNode node in node.ChildNodes)
  {
    PrintRecursive(node);
  }
}

TreeView.Nodes gives a collection of TreeNode objects that represents the root nodes in the TreeView control.

To access the child nodes of a root node, use the ChildNodes property of the node.

e.g. using for loop

void  PrintRecursive(TreeNode node)
{
  for(int i=0; i <node.ChildNodes.Count; i++)
  {
    PrintRecursive(node.ChildNodes[i]);
  }
}

or using foreach

void  PrintRecursive(TreeNode node)
{
  foreach(TreeNode node in node.ChildNodes)
  {
    PrintRecursive(node);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文