473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

New foreach syntax?

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
4 3793
"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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
14
by: Josh Ferguson | last post by:
I don't believe a syntax driven equivalent exists for this, but I just thought it would be neat if you could use foreach to do something like this: foreach (Object x in collection1, collection2)...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
2
by: Jason Ourscene | last post by:
I'm using the simplexml class to parse through some xml. when i use the standard foreach loop syntax, i get the expected results. <?php foreach ($xml->item as $artist) { echo $artist->title "<br...
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.