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 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
"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
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
"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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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...
|
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)...
|
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(
|
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 :...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |