The constructor does not introduce a C # function

I have a code. the constructor should introduce the GetItems function, but when I set a breakpoint, it just doesn't 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; } } } } 
+6
source share
1 answer

When you use yield , it means deferred execution, so you need to list the variable a , for example:

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

You can also use ToArray() , whatever, you just need to list your IEnumerable .

+11
source

Source: https://habr.com/ru/post/1012478/


All Articles