New foreach syntax?

  • Thread starter Thread starter Niall
  • Start date Start date
N

Niall

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
 
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) Console.WriteLine(st);

-mbray
 
Niall said:
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, ;)
 
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 said:
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) Console.WriteLine(st);

-mbray
 
Niall said:
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

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) Console.WriteLine(st);

-mbray

 
Back
Top