M.G. M.G. - 14 days ago 5
C# Question

constructor do not enter a function C#

i have code. the constructor should enter the

GetItems
function but when i place breakpoint, it simply do not stop.

what is the problem?

namespace Storehouse {
public partial class MainForm : Form {
public MainForm() {
InitializeComponent();
var a = GetItems(fILEToolStripMenuItem);

}

public IEnumerable<ToolStripMenuItem> GetItems(ToolStripMenuItem item) {
foreach (ToolStripMenuItem dropDownItem in item.DropDownItems) {
if (dropDownItem.HasDropDownItems) {
foreach (ToolStripMenuItem subItem in GetItems(dropDownItem)) {
yield return subItem;
}
yield return dropDownItem;
}

}

}
}

Answer

When you're using yield, it means deferred execution, so you need to enumerate your variable a, like this:

var a = GetItems(fILEToolStripMenuItem).ToList();

You can also use ToArray(), whatever, you simply need to enumerate your IEnumerable.

Comments