Tag: foreach

在MenuStrip中预览每个Subitem

我想得到我的MenuStrip所有SubItems ,所以我可以一次更改它们。 我正在尝试以下内容,但它们无法正常工作: foreach (ToolStripMenuItem toolItem in menuStrip1.DropDownItems) { //Do something with toolItem here } 有人可以帮我编写一个好的foreach loop来从MenuStrip获取所有SubMenuItems(DropDownItems)吗? 编辑现在尝试使用以下Recursive method : private void SetToolStripItems(ToolStripItemCollection dropDownItems) { try { foreach (object obj in dropDownItems) { if (obj.GetType().Equals(typeof(ToolStripMenuItem))) { ToolStripMenuItem subMenu = (ToolStripMenuItem)obj; if (subMenu.HasDropDownItems) { SetToolStripItems(subMenu.DropDownItems); } else { } } } } catch { } }

C#ListView项目图像

如何使用foreach语句将图像(指定的图像)添加到listview中,例如: foreach(Video entry in videoFeed.Entries) { listview1.items.add(entry); listview1.items.image(imageURL); }

foreach(… in …)或.ForEach(); 这就是问题

可能重复: C#foreach vs functional each 这是关于编码可读性的问题。 我有一个XDocument和一个List包含我需要屏蔽的敏感信息的元素名称(在本例中用下划线替换)。 XDocument xDoc; List propertiesToMask; 这可以用两种方式编写,使用传统的foreach循环,或使用带有lamba语法的.ForEach方法。 foreach (string propertyToMask in propertiesToMask) { foreach (XElement element in xDoc.Descendants(propertyToMask)) { element.SetValue(new string(‘_’, element.Value.Length)); } } 要么 propertiesToMask .ForEach(propertyToMask => xDoc.Descendants(propertyToMask).ToList() .ForEach(element => element.SetValue(new string(‘_’, element.Value.Length)))); 您认为哪种方法最易读,为什么? 如果您更喜欢第二个示例,您将如何呈现它以获得最大可读性?

C#newbie:找出foreach块中的索引

我有一个foreach块,我想为跟踪调试目的绘制foreach内部步骤的索引。 作为一个C#新手我做如下: int i = 1; foreach (x in y) { … do something … WriteDebug(“Step: “+i.ToString()); i++; } 我想知道是否有任何方法可以获得当前步骤索引的值而无需为此目的明确创建变量。 编辑:为了澄清,我显然熟悉for循环的选项,但它不是我正在经历的数组,而是一个无序的集合。 编号的原因仅仅是为了显示调试级别的进度而不是其他任何内容。

在所有DataTable列中查找字符串

我试图找到一种快速的方法来在所有数据表列中找到一个字符串! 随后没有工作,因为我想在所有列值内搜索。 string str = “%whatever%”; foreach (DataRow row in dataTable.Rows) foreach (DataColumn col in row.ItemArray) if (row[col].ToString() == str) return true;

在C#中Foreach结构奇怪的编译错误

namespace MyNamespace { public struct MyStruct { public string MyString; public int MyInt; public bool MyBool; } public class MyClass { private List MyPrivateVariable; public List MyVariable { get { if (MyPrivateVariable == null) { MyPrivateVariable = new List(); MyPrivateVariable.Add(new MyStruct()); MyPrivateVariable.Add(new MyStruct()); } return MyPrivateVariable; } } public void MyLoop() { foreach (MyStruct ms […]

为什么foreach跳过对接口类型的编译时类型检查?

当我在C#中使用foreach循环时,如果项类型是接口类型,则似乎不执行编译时类型检查。 例如 class SomeClass {} interface SomeInterface {} IEnumerable stuff; foreach(SomeInterface obj in stuff) { // This compiles – why!? } 这将很乐意编译并在运行时导致exception,当在编译时清楚这没有意义。 如果我将项类型从SomeInterface为另一个类,则会恢复编译时类型检查: IEnumerable stuff; foreach(Random obj in stuff) { // This doesn’t compile – good! } 当项类型是接口时,为什么没有编译时类型检查? (这在Visual Studio 2008中的.NET 3.5 SP1中发生)

当它可以被修改时经历一个foreach?

我想在取出foreach循环的成员时执行foreach循环,但这会抛出错误。 我唯一的想法是在这个循环中创建另一个列表以找到要删除的切片,并循环遍历新列表以从Pizza中删除项目。 foreach(var Slice in Pizza) { if(Slice.Flavor == “Sausage”) { Me.Eat(Slice); //This removes an item from the list: “Pizza” } }

为什么foreach无法找到我的GetEnumerator扩展方法?

我试图使一些代码更具可读性。 例如foreach(var row in table) {…}而不是foreach(DataRow row in table.Rows) {…} 。 为此,我创建了一个扩展方法: namespace System.Data { public static class MyExtensions { public static IEnumerable GetEnumerator( this DataTable tbl ) { foreach ( DataRow r in tbl.Rows ) yield return r; } } } 但是编译器仍然抛出foreach statement cannot operate on variables of type ‘System.Data.DataTable’ because ‘System.Data.DataTable’ does not […]

为什么foreach循环在某些情况下不起作用?

我正在使用foreach循环来处理要处理的数据列表(一旦处理就删除了所述数据 – 这是在锁内)。 此方法偶尔会导致ArgumentException。 抓住它会很昂贵,所以我试着追查这个问题,但我无法弄明白。 我已经切换到for循环,问题似乎已经消失了。 有人能解释发生了什么吗? 即使有exception消息,我也不太了解幕后发生了什么。 为什么for循环显然有效? 我是否设置了foreach循环错误或什么? 这几乎是我的循环设置方式: foreach (string data in new List(Foo.Requests)) { // Process the data. lock (Foo.Requests) { Foo.Requests.Remove(data); } } 和 for (int i = 0; i < Foo.Requests.Count; i++) { string data = Foo.Requests[i]; // Process the data. lock (Foo.Requests) { Foo.Requests.Remove(data); } } 编辑:for *循环是这样的设置,如下所示: […]