foreach

C#: Can I have a method return IEnumerator<T> and use it in a foreach loop?

Howdy, I need to set the height of every textbox on my form, some of which are nested within other controls. I thought I could do something like this: private static IEnumerator<TextBox> FindTextBoxes(Control rootControl){ foreach (Control control in rootControl.Controls) { if (control.Controls.Count > 0) { ...

foreach access the index or an associative array

I have the following code snippet. $items['A'] = "Test"; $items['B'] = "Test"; $items['C'] = "Test"; $items['D'] = "Test"; $index = 0; foreach($items as $key => $value) { echo "$index is a $key containing $value\n"; $index++; } Expected output: 0 is a A containing Test 1 is a B containing Test 2 is a C containing Test 3 is a...

What's the fastest way to copy the values and keys from one dictionary into another in C#?

There doesn't seem to be a dictionary.AddRange() method. Does anyone know a better way to copy the items to another dictionary without using a foreach loop. I'm using the System.Collections.Generic.Dictionary. This is for .NET 2.0. ...

How can I enumerate all *.exes and the details about each?

Something like: //Get all search data $search = new search('C:\', '*.exe'); while($item = $search->next()) { $details = $item->getDetails(); append_details('C:\files.txt', implode_details($details)); } But in NSIS (http://nsis.sourceforge.net/) ...

Does Class need to implement IEnumerable to use Foreach

This is in C#, I have a class that I am using from some else's DLL. It does not implement IEnumerable but has 2 methods that pass back a IEnumerator. Is there a way I can use a foreach loop on these. The class I am using is sealed. ...

How to find the foreach index

Hi all, Is it possible to find the foreach index? in a "for" loop as follows: for($i = 0; $i < 10; ++$i){ echo $i.' '; } $i will give you the index. Do I have to use the for loop or is there some way to get the index in the foreach loop? ...

Is the order of objects returned by FOREACH stable?

Is it safe to assume that two itterations over the same collection will return the objects in the same order? Obviously, it is assumed that the collection has not otherwise been changed. ...

Visual c++ "for each" portability

I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as well?) supports for each syntax on stl lists et al to facilitate iteration. For example: list<Object> myList; for each (Object o in myList) { o.foo(); } I was very happy to discover it, but I'm concerned about portability for the dreaded day when ...

LINQ equivalent of foreach for IEnumerable<T>

I'd like to do the equivalent of the following in LINQ, but I can't figure out how: IEnumerable<Item> items = GetItems(); items.ForEach(i => i.DoStuff()); What is the real syntax? ...

Why does my attempt to trim strings in a List<string> not appear to work?

I tried the following code in LINQPad and got the results given below: List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump(); listFromSplit.ForEach(delegate(string s) { s.Trim(); }); listFromSplit.Dump(); "a" and " b" so the letter b didn't get the white-space removed as I was expecting...? A...

Does foreach always create a copy on a none reference in PHP?

I'm wondering if PHP has this optimization built in. Normally when you call foreach without using a reference it copies the passed array and operates on it. What happens if the reference count to that array is only 1? Say for example if getData returns some array of data. foreach(getData() as $data) echo $data; Since the array ...

How to Convert all strings in List<string> to lower case using LINQ?

Hi all, I saw a code snippet yesterday in one of the responses here on StackOverflow that intrigued me. It was something like this: List<string> myList = new List<string> {"aBc", "HELLO", "GoodBye"}; myList.ForEach(d=>d.ToLower()); I was hoping I could use it to convert all items in myList to lowercase. However, it doesn't happen...

Can you enumerate a collection in C# out of order?

Is there a way to use a foreach loop to iterate through a collection backwards or in a completely random order? ...

Changing item in foreach thru method.

Let's start with the following snippet: Foreach(Record item in RecordList){ .. item = UpdateRecord(item, 5); .. } The UpdateRecode function changes some field of item and returns the altered object. In this case the compiler throws an exception saying that the item can not be updated in a foreach iteration. Now the UpdateRecor...

C#, For Loops, and speed test... Exact same loop faster second time around?

public Int64 ReturnDifferenceA() { User[] arrayList; Int64 firstTicks; IList<User> userList; Int64 secondTicks; System.Diagnostics.Stopwatch watch; userList = Enumerable .Range(0, 1000) .Select(currentItem => new User()).ToList(); arrayList = userList.ToArray(); watch = new Stopwatch(); wa...

When to use FOR-CASE (Foreach/switch in C#)?

I've found what seems to be the C# equivalent of a FOR-CASE structure in a proyect I'm working on: foreach (string param in params.Split(';')) { string[] parts = param.Split('='); string key = parts[0].Trim().ToLower(); string value = parts[1].Trim(); switch (key) { case "param1": this.param1 = value; break; ...

How to use foreach keyword on custom Objects in C#

Can someone share a simple example of using the foreach keyword with custom objects? ...

How do you find the last loop in a For Each (VB.NET)?

How can I determine if I'm in the final loop of a For Each statement in VB.NET? ...

Why foreach statements accept objects that implement the 'Collection' pattern instead of accept only objects that implement IEnumerable?

Many people ask me why, and I don`t have a good answer for them. Obviously there is a good reason. Does anyone know it? I searched here and found this question. It explains how it works, but not why. ...

How do foreach loops work in C#?

I don't understand which types of classes can use foreach loops. Please help! ...