By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

New foreach syntax?

P: n/a
Ok, maybe this is getting too lazy and too demanding, but hey, I thought I'd
see what people thought about it.

Would anyone else find a syntax like:

foreach (string Name in CompanyNames and EmployeeNames and ...)
{
etc
}

useful? I know that on some collections, you can append, insert etc. But
it's not always the case that you have such collections when you are
enumerating. Also, often you do not want to change one of the original
collections by appending the elements from the other collection.

So you are left with two choices. Make a new collection and stuff in all the
combined elements you want, which will be a performance hit. Or write the
foreach loop once for each collection you want to go through, which results
in code duplication, increasing likelihood of refactoring causing bugs. I
think the "and" syntax would provide a simple, succinct and very readable
alternative to this...

Unless, of course, I'm on crack and there's some easy way to do this that
I've overlooked?

Niall
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Niall" <as**@me.com> wrote in
news:Ok**************@TK2MSFTNGP12.phx.gbl:
foreach (string Name in CompanyNames and EmployeeNames and ...)
{
etc
}
[snip]
Unless, of course, I'm on crack and there's some easy way to do this
that I've overlooked?


How about:

IEnumerable[] ies = new IEnumerable[] { CompanyNames, EmployeeNames };
for(int i=0;i<ies.Length; i++)
foreach(string st in ies[i]) Console.WriteLine(st);

-mbray
Nov 15 '05 #2

P: n/a

"Niall" <as**@me.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
Ok, maybe this is getting too lazy and too demanding, but hey, I thought I'd see what people thought about it.

Would anyone else find a syntax like:

foreach (string Name in CompanyNames and EmployeeNames and ...)
{
etc
}

useful? I know that on some collections, you can append, insert etc. But
it's not always the case that you have such collections when you are
enumerating. Also, often you do not want to change one of the original
collections by appending the elements from the other collection.

So you are left with two choices. Make a new collection and stuff in all the combined elements you want, which will be a performance hit. Or write the
foreach loop once for each collection you want to go through, which results in code duplication, increasing likelihood of refactoring causing bugs. I
think the "and" syntax would provide a simple, succinct and very readable
alternative to this...

Unless, of course, I'm on crack and there's some easy way to do this that
I've overlooked? maybe somthing like:

public class MultiEnumerator : IEnumerator
{
public MultiEnumerator(params IEnumerable[] enumerators)
{
IList enums = new ArrayList();
foreach (IEnumerable enumerable in enumerators)
{
enums.Add(enumerable.GetEnumerator());
}
}
IEnumerator[] enumerators;
int currentEnum=0;
public void Reset()
{
// TODO: Add MultiEnumerator.Reset implementation
currentEnum = 0;
foreach (IEnumerator enumerator in enumerators)
{
enumerator.Reset();
}
}

public object Current
{
get
{
// TODO: Add MultiEnumerator.Current getter implementation
return enumerators[currentEnum].Current;
}
}

public bool MoveNext()
{
// TODO: Add MultiEnumerator.MoveNext implementation
bool next = enumerators[currentEnum].MoveNext();
if (!next)
{
currentEnum++;
if (enumerators.Length >= currentEnum)
return MoveNext();
else
return false;
}
return true;
}

}

Note, I didn't test this, so if I made a mistake(probably did) lemme know.
Cubs game is still going and I just don't have the time to check it, ;)
Niall

Nov 15 '05 #3

P: n/a
Yeah, I've since had this solution (as well as Daniel's IEnumerator
implementation) suggested to me by a guy at work. I don't mind this one,
though the IEnumerator implementation is a fair bit of work. My argument was
that foreach exists simply as a convenience over a straight for loop... and
the "and" solution was much more convenient than the other solutions. I was
laughed at! :P

Niall

"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message
news:Xn*******************************@207.46.248. 16...
"Niall" <as**@me.com> wrote in
news:Ok**************@TK2MSFTNGP12.phx.gbl:
foreach (string Name in CompanyNames and EmployeeNames and ...)
{
etc
}

[snip]

Unless, of course, I'm on crack and there's some easy way to do this
that I've overlooked?


How about:

IEnumerable[] ies = new IEnumerable[] { CompanyNames, EmployeeNames };
for(int i=0;i<ies.Length; i++)
foreach(string st in ies[i]) Console.WriteLine(st);

-mbray

Nov 15 '05 #4

P: n/a

"Niall" <as**@me.com> wrote in message
news:eb*************@TK2MSFTNGP09.phx.gbl...
Yeah, I've since had this solution (as well as Daniel's IEnumerator
implementation) suggested to me by a guy at work. I don't mind this one,
though the IEnumerator implementation is a fair bit of work. My argument was that foreach exists simply as a convenience over a straight for loop... and the "and" solution was much more convenient than the other solutions. I was laughed at! :P
It really is a form of convience, but an enumerator can be a little more
complicated than a simple for. Anyway, there are a number of things that
have been suggested for foreach, from support for adding & removing objects
from the collection, language level support for sortable enumerators, etc.
The question is really are they worth it, or does the simple IEnumerator
wrapper idea do the job with less fuss. Niall

"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message news:Xn*******************************@207.46.248. 16...
"Niall" <as**@me.com> wrote in
news:Ok**************@TK2MSFTNGP12.phx.gbl:
foreach (string Name in CompanyNames and EmployeeNames and ...)
{
etc
}

[snip]

Unless, of course, I'm on crack and there's some easy way to do this
that I've overlooked?


How about:

IEnumerable[] ies = new IEnumerable[] { CompanyNames, EmployeeNames };
for(int i=0;i<ies.Length; i++)
foreach(string st in ies[i]) Console.WriteLine(st);

-mbray


Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.