Jump to content
  • Advertisement
Sign in to follow this  

C# ForEach vs array indexes (indices?)

This topic is 3092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good afternoon all. In the interests of transitioning to a real(ish) C# coder rather than just changing my C++ style enough to compile, I was wondering how I could use ForEach instead of the following index-based for loop

for (int i = 0; i < weights.dimension; i++){
     weights.elements+=(alpha * neighbourhood * elementDistances.elements);
}

I can do a standard ForEach well enough, but here I'm using
i
to acces the relevant element of two different arrays I'm not sure how to carry across the technique. Is it possible / sensible / worth the bother?

Share this post


Link to post
Share on other sites
Advertisement
There might be a crafty linq solution for it, but I'd just not bother. Or using foreach for the int and a range... enh. Just use the for loop.

Share this post


Link to post
Share on other sites
If you need to index two lists, just keep doing it the way you're doing it. You don't need to exclusively use foreach loops to feel like a real C# coder; sometimes a standard for loop really is the best way to go.

Share this post


Link to post
Share on other sites
Accessing corresponding values from two collections in parallel in C# is one of the classic examples of when to use indices instead of foreaches. If you really really wanted to, you could write an iterator that would allow you to use it in a foreach loop. This is not quite as hard as it sounds, but it's most suitable when:

a) The object types in the two collections are classes and not structs
b) You're only accessing member variables / methods on the objects, not replacing their entries in the collections with new objects.


Then, you can use the amazing "yield return" statement in C# to automatically create an iterator for you, and do something like:



public static class Parallel<T,U>
{
public struct Pair
{
Pair(T first, U second)
{
this.first = first;
this.second = second;
}
T first;
U second;
}

static IEnumerable<Pair> Enumerator(IList<T> tlist, IList<U> ulist)
{
for (int i=0; i < tlist.Count; ++i)
yield return Pair(tlist, ulist);
}
}

public class Test
{

private class Foo { int foo; }
private class Bar { int bar; }

List<Foo> foos;
List<Bar> bars;

public void doStuff()
{
foreach (Parallel<Foo,Bar>.Pair entry in Parallel<Foo,Bar>.Enumerator(foos, bars))
{
entry.first.foo = entry.second.bar;
}
}
}



There's some overhead in doing things this way, and not too much advantage, but it's a useful concept illustration anyway because yield return really comes in handy sometimes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!