473,655 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Silly suggestion - thinking aloud

I've been briefly musing on what is probably a pretty silly idea, but
one which would no doubt benefit from being discussed and thoroughly
shot down in flames rather than being allowed to fester in my head.

Quite a lot of the time when I would logically use foreach in C#, I
can't because I want to do something extra: usually either delete the
current element from the backing collection or possibly change it. As
it is, I have to use a plain for loop or whatever. It works, but it's
not as pleasant as it might be.

So, here's some bizarre straw-man syntax for an alternative:

foreach (string x in myArrayList)
{
if (CheckForRemove (x))
enumerator(x).R emove();
else if (CheckForFoo(x) )
enumerator(x).S et("foo");
else
Console.WriteLi ne ("Didn't change {0}", x);
}

There would be two extra interfaces as well as IEnumerator:
IRemovableEnume rator and IMutableEnumera tor (or something like that).
The enumerator returned by an array would implement IMutableEnumera tor,
whereas the enumerator returned by an ArrayList would implement
IMutableEnumera tor and IRemovableEnume rator (if the ArrayList itself
were not read-only, of course). A collection could continue to be
enumerated if it had only been modified by the enumerator doing the
enumeration since the last step (rather than saying it couldn't have
been modified at all).

Any foreach loop which included the Remove action would cast the
enumerator to IRemovableEnume rator before launching into the main loop,
and likewise for Set/IMutableEnumera tor.
As an example of a real-life use I'd have liked for this recently, I
needed to do:

for (int i=0; i < names.Count; i++)
{
names[i]=names[i].Trim();
}

where I might have been able to do:

foreach (string name in names)
{
enumerator(name ).Set(name.Trim ());
}

which I think is nicer.

Now for the problems:

o The syntax is horrible at the moment, partly because there may be
more than one enumerator going at a time so you need to identify
which enumerator you mean.

o There would need to be either an overloaded or new keyword to support
this - and a new keyword would make it a breaking change. Aargh!

o The check for an enumerator being mutable/removable is done at
runtime rather than compile time (and must be) - not nice.

o Some enumerations might be able to support IMutableEnumera tor but
only in a weird way - if you're enumerating over a hashtable's keys,
what does it mean if you set it to something else? I guess this isn't
so bad, as you'd just not implement IMutableEnumera tor in those
circumstances - but care would be needed.

There are bound to be more problems, and there may even be more
benefits or (more likely) much better ways of tackling the idea -
suggestions?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #1
15 1470
few initial comments inline.
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
I've been briefly musing on what is probably a pretty silly idea, but
one which would no doubt benefit from being discussed and thoroughly
shot down in flames rather than being allowed to fester in my head.

Quite a lot of the time when I would logically use foreach in C#, I
can't because I want to do something extra: usually either delete the
current element from the backing collection or possibly change it. As
it is, I have to use a plain for loop or whatever. It works, but it's
not as pleasant as it might be.

I have to say, this is a feature i'd like too, lack of a capacity to modify
the enumerated object in a foreach is really annoying and often ends up
causing me to either use more for loops then i'd like, or seperating
operations in a manner i don't care for either.
So, here's some bizarre straw-man syntax for an alternative:

foreach (string x in myArrayList)
{
if (CheckForRemove (x))
enumerator(x).R emove();
else if (CheckForFoo(x) )
enumerator(x).S et("foo");
else
Console.WriteLi ne ("Didn't change {0}", x);
}

Thing here is intellisense (i think the language should consider
intellisense as much as intellisense should consider the language, in these
days). There would be no simple way to determine if .Remove() & .Set() is
possible under this current syntax, as you noted the check would have to be
applied at runtime.
While documentation is a good thing, in-code documentation is always a help.
Perhaps something else is needed, maybe:
foreach removable (string x in myArrayList)
{
//do your stuff
}

That would allow you to force removable support(throwin g an exception if its
not there, better to find that in the foreach statement, before you do
anything, i think), give intellisense a clue as to what your doing, etc. It
would also allow for extended safety, even if you have a removable
enumerator, without the removable keyword, you couldn't perform a remove
accidentally.

One explicit problem (or possibly a bonus) is that the removeable clause
could load a different enumerator causing unexpected results(perhaps ONLY
returning objects than can be removed safely, etc). That would be a matter
of design. And arguments could go either way.

anyway it is still not very pretty syntax either.
There would be two extra interfaces as well as IEnumerator:
IRemovableEnume rator and IMutableEnumera tor (or something like that).
The enumerator returned by an array would implement IMutableEnumera tor,
whereas the enumerator returned by an ArrayList would implement
IMutableEnumera tor and IRemovableEnume rator (if the ArrayList itself
were not read-only, of course). A collection could continue to be
enumerated if it had only been modified by the enumerator doing the
enumeration since the last step (rather than saying it couldn't have
been modified at all).

Any foreach loop which included the Remove action would cast the
enumerator to IRemovableEnume rator before launching into the main loop,
and likewise for Set/IMutableEnumera tor.
As an example of a real-life use I'd have liked for this recently, I
needed to do:

for (int i=0; i < names.Count; i++)
{
names[i]=names[i].Trim();
}

where I might have been able to do:

foreach (string name in names)
{
enumerator(name ).Set(name.Trim ());
}

which I think is nicer.

Now for the problems:

o The syntax is horrible at the moment, partly because there may be
more than one enumerator going at a time so you need to identify
which enumerator you mean.

This is kinda nasty, and hopefully someone else would have a suggestion as
to enhance the syntax, currently enumerator(x) works, but its not very
pretty. Naming enumerators could work but that would again begin to expand
code verbosity, possibly needlessly

foreach removable (string s in myArrayList) enumerator X
{
X.Remove(s);
//etc, etc
}

I don't find that pretty either, but maybe someone can morph it into a nice
syntax...
o There would need to be either an overloaded or new keyword to support
this - and a new keyword would make it a breaking change. Aargh!

Possibly more than one keyword, all in all.
I wonder how this would interact with iterators.
o The check for an enumerator being mutable/removable is done at
runtime rather than compile time (and must be) - not nice.

I addressed some bits of this above, which would allow for slightly better
runtime checking in a manner similar to how foreach works now.
o Some enumerations might be able to support IMutableEnumera tor but
only in a weird way - if you're enumerating over a hashtable's keys,
what does it mean if you set it to something else? I guess this isn't
so bad, as you'd just not implement IMutableEnumera tor in those
circumstances - but care would be needed.

There are bound to be more problems, and there may even be more
benefits or (more likely) much better ways of tackling the idea -
suggestions?

On top of that a type-filtered foreach would be nice, in mixed collections
it is very annoying to have to do

foreach (object o in objects)
{
if (o is string) {
//do string work
}
}

that too probably has alot of troubles, but i figured i would throw that old
idea into the fray here as well, ;)
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #2
If there was an enumerator function that gave access to the underlying
enumeration object wouldn't that be sufficient without further language
changes. Implementers could simply implement any interface they wished as
well as IEnumerator. This might make the code a bit more verbose but I think
better than embedding too many specifics in the language.

e.g.

foreach (string str in myCollection){
object o= enumerator(str) ;
if (o is IMyInterface)
((IMyInterface) o).DoSomething( );
}

Regarding deletion, I always implement enumerators to work backwards which
helps sometimes but not always. Doesn't help with the builtin ones of
course.

Cheers

Doug Forster

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
I've been briefly musing on what is probably a pretty silly idea, but
one which would no doubt benefit from being discussed and thoroughly
shot down in flames rather than being allowed to fester in my head.

Quite a lot of the time when I would logically use foreach in C#, I
can't because I want to do something extra: usually either delete the
current element from the backing collection or possibly change it. As
it is, I have to use a plain for loop or whatever. It works, but it's
not as pleasant as it might be.

So, here's some bizarre straw-man syntax for an alternative:

foreach (string x in myArrayList)
{
if (CheckForRemove (x))
enumerator(x).R emove();
else if (CheckForFoo(x) )
enumerator(x).S et("foo");
else
Console.WriteLi ne ("Didn't change {0}", x);
}

There would be two extra interfaces as well as IEnumerator:
IRemovableEnume rator and IMutableEnumera tor (or something like that).
The enumerator returned by an array would implement IMutableEnumera tor,
whereas the enumerator returned by an ArrayList would implement
IMutableEnumera tor and IRemovableEnume rator (if the ArrayList itself
were not read-only, of course). A collection could continue to be
enumerated if it had only been modified by the enumerator doing the
enumeration since the last step (rather than saying it couldn't have
been modified at all).

Any foreach loop which included the Remove action would cast the
enumerator to IRemovableEnume rator before launching into the main loop,
and likewise for Set/IMutableEnumera tor.
As an example of a real-life use I'd have liked for this recently, I
needed to do:

for (int i=0; i < names.Count; i++)
{
names[i]=names[i].Trim();
}

where I might have been able to do:

foreach (string name in names)
{
enumerator(name ).Set(name.Trim ());
}

which I think is nicer.

Now for the problems:

o The syntax is horrible at the moment, partly because there may be
more than one enumerator going at a time so you need to identify
which enumerator you mean.

o There would need to be either an overloaded or new keyword to support
this - and a new keyword would make it a breaking change. Aargh!

o The check for an enumerator being mutable/removable is done at
runtime rather than compile time (and must be) - not nice.

o Some enumerations might be able to support IMutableEnumera tor but
only in a weird way - if you're enumerating over a hashtable's keys,
what does it mean if you set it to something else? I guess this isn't
so bad, as you'd just not implement IMutableEnumera tor in those
circumstances - but care would be needed.

There are bound to be more problems, and there may even be more
benefits or (more likely) much better ways of tackling the idea -
suggestions?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3
Main problem with this would be, for example you wanted deletion, you may
have a dozen different interfaces with a delete a member, one in different
third party application. So at the least a set of standard IRemovable type
interfaces would have to exist, even if not as a language change.

For removing, removing a further down object could be troublesome, but
replacing it with a place holder until the enumerator wraps up could fix
that (it would be easy to skip based on a NullObject placeholder or a skip
list).

Other problems include addition of elements. If your gonna have removable
foreach, you will need an addable one. In sorted lists adding an object can
be a real pain, because its location is not easily predicted. Should an
addition be loaded into the enumerator later on?

Adding such features to the language itself is not nessecerily a bad thing
if done properly. Forcing specifics into a language is often not a good
idea, but sometimes language level extentsions provide the better solution
for designers.

foreach access modifiers(in this case, can add, can remove, etc), sorts,
filters, etc at a language level could result in a much more standard method
of doing things, with explicit compile time type safety, instead of hoping
people follow uninforced interface guidelines and using runtime type checks.
Downside, of course, is it could cause other languages headaches in having
to support the new functionality, it would add complexity to both the
compiler and the understanding of the language, and writing enumerators
would get more difficult. Iterators may actually make writing such things
much simpiler, depending on the design.

"Doug Forster" <doug_ZAPTHIS_A T_TONIQ_ZAPTHIS _co.nz> wrote in message
news:uK******** ******@TK2MSFTN GP12.phx.gbl...
If there was an enumerator function that gave access to the underlying
enumeration object wouldn't that be sufficient without further language
changes. Implementers could simply implement any interface they wished as
well as IEnumerator. This might make the code a bit more verbose but I think better than embedding too many specifics in the language.

e.g.

foreach (string str in myCollection){
object o= enumerator(str) ;
if (o is IMyInterface)
((IMyInterface) o).DoSomething( );
}

Regarding deletion, I always implement enumerators to work backwards which
helps sometimes but not always. Doesn't help with the builtin ones of
course.

Cheers

Doug Forster

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
I've been briefly musing on what is probably a pretty silly idea, but
one which would no doubt benefit from being discussed and thoroughly
shot down in flames rather than being allowed to fester in my head.

Quite a lot of the time when I would logically use foreach in C#, I
can't because I want to do something extra: usually either delete the
current element from the backing collection or possibly change it. As
it is, I have to use a plain for loop or whatever. It works, but it's
not as pleasant as it might be.

So, here's some bizarre straw-man syntax for an alternative:

foreach (string x in myArrayList)
{
if (CheckForRemove (x))
enumerator(x).R emove();
else if (CheckForFoo(x) )
enumerator(x).S et("foo");
else
Console.WriteLi ne ("Didn't change {0}", x);
}

There would be two extra interfaces as well as IEnumerator:
IRemovableEnume rator and IMutableEnumera tor (or something like that).
The enumerator returned by an array would implement IMutableEnumera tor,
whereas the enumerator returned by an ArrayList would implement
IMutableEnumera tor and IRemovableEnume rator (if the ArrayList itself
were not read-only, of course). A collection could continue to be
enumerated if it had only been modified by the enumerator doing the
enumeration since the last step (rather than saying it couldn't have
been modified at all).

Any foreach loop which included the Remove action would cast the
enumerator to IRemovableEnume rator before launching into the main loop,
and likewise for Set/IMutableEnumera tor.
As an example of a real-life use I'd have liked for this recently, I
needed to do:

for (int i=0; i < names.Count; i++)
{
names[i]=names[i].Trim();
}

where I might have been able to do:

foreach (string name in names)
{
enumerator(name ).Set(name.Trim ());
}

which I think is nicer.

Now for the problems:

o The syntax is horrible at the moment, partly because there may be
more than one enumerator going at a time so you need to identify
which enumerator you mean.

o There would need to be either an overloaded or new keyword to support
this - and a new keyword would make it a breaking change. Aargh!

o The check for an enumerator being mutable/removable is done at
runtime rather than compile time (and must be) - not nice.

o Some enumerations might be able to support IMutableEnumera tor but
only in a weird way - if you're enumerating over a hashtable's keys,
what does it mean if you set it to something else? I guess this isn't
so bad, as you'd just not implement IMutableEnumera tor in those
circumstances - but care would be needed.

There are bound to be more problems, and there may even be more
benefits or (more likely) much better ways of tackling the idea -
suggestions?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 15 '05 #4
james <no****@hyperco n.net> wrote:
I would say MS should just add the ability to remove from IEnumerator
and forget the special form of IRemovableEnume rator.
Would Remove then throw an exception if the collection didn't support
it (eg arrays, read-only collections, etc)?
But I can see
that it would probably lead to other problems I can't think of off hand.
Maybe they could just add a special Remove method like
MarkForRemovalW hileInForEach()
which would actually just mark it for removal until the loop exits


That sounds like it's asking for trouble to me - deferring things is
always going to have potentially nasty side-effects, unless the
programmer has a *really* good idea what's going on behind the scenes.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
inline
"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:uK******** ******@TK2MSFTN GP09.phx.gbl...
Sorry if I miss the point,
however I can't get myself away from commenting.

Personally I think problem with deletion of items from collection/array
during foreach loop is compiler problem and partly problem of IEnumerator
implementations .

Whatever direction you enumerate, removal can be either programmed in
low-level MSIL, either in implementation completely transparently for
programmer and quite efficiently - and most important, without any issues
like "collection can't be modified". At least I don't see such issues. I
don't know why this was not implemented from the very start. Maybe framework designers know. I was very disappointed to see these exceptions when I
started with Beta2. I do not understand really, why we have to live with
them in 1.1 and it looks like further on.

I know that Eric mentioned that we don't want collections to change under
us, however when you call collection.Remo ve or RemoveAt method you
effectively change collection. So I miss the point of Eric's argument. And
correspondingly I do not see the need for additional interfaces or classes, which make collection enumeration more obscure and complex than it is
already.

Consider if your removal code was to add or remove an object 10 or 20 units
behind your current position. If the code wasn't carefully written, then the
index your accessing in your enumerator will skip or repeat a value and
probably cause trouble.
This is why ruling out modification of an object is a good idea, however,
allowing objects to be modified *THROUGH* the enumerator could work in some
situations.

However, another case comes about in Hashtables, SortedLists and the like,
where there is no guarenteed or a dynamic order, adding or removing a item
could throw off the enumeration in an unpredictable way, forcing the
IEnumerator writer to create a much more complex implementation, if they can
support it at all.

Because of this, the IEnumerator interface was written to be as simple as
possible and to cover only the very basic functionality. If Remove was to be
added, i can almost guarentee it will result in a new interface, in part
because modifying IEnumerator would break a great deal of code, but also
because Remove would not work in all cases safetly. It would have to be
selectivly applied, that makes IRemovableEnume rator a workable solution
where modifying IEnumerator would not be.
I would go in the direction of proper implementation of IEnumerators. Remove method must modify collection internal structure in such way, that
IEnumerator.Nex t or .Prev or whatever is used to run through elements in
foreach loop, will work properly. Intuitively we know that if we use foreach and remove first element, we expect next loop to be performed on second
element, which is next after first. If first element is the only one,
foreach should complete peacefully. Etc. It is logical for me. Current
exception seems illogical and indicates to me that implementation is
deficient.

Yes, there might be problems with sloppy coding. However we do not fight for development of support classes or interfaces, which will help to avoid
situations like

myobj=null;
myobj.mymethod( );

which are causing null reference exception. Even if it is maybe possible to invent such object or INullCatcher, that myobj will catch such situations
after making it null, and maybe even do something useful.

Did I miss something important?

Rgds

Nov 15 '05 #6
See inline
Consider if your removal code was to add or remove an object 10 or 20 units behind your current position. If the code wasn't carefully written, then the index your accessing in your enumerator will skip or repeat a value and
probably cause trouble.
That's why I mentioned the problem with calling method of disposed/nulled
object - this also might happen after several lines or in another thread
etc. I don't think that language constructs might help with this kind of
errors.
However, another case comes about in Hashtables, SortedLists and the like,
where there is no guarenteed or a dynamic order, adding or removing a item
could throw off the enumeration in an unpredictable way, forcing the
IEnumerator writer to create a much more complex implementation, if they can support it at all.
I am not sure that collection.Add is really useful in foreach. Do you have
some specific example to discuss?
However even for Add for every specific collection there are logical
scenarios.
Remove is logical always for me. I haven't seen (yet) sound example, which
demonstrates unpredictabilit y here. That's why I pushed in the thread here
with my comments.

Because of this, the IEnumerator interface was written to be as simple as
possible and to cover only the very basic functionality. If Remove was to be added, i can almost guarentee it will result in a new interface, in part
because modifying IEnumerator would break a great deal of code, but also
because Remove would not work in all cases safetly. It would have to be
selectivly applied, that makes IRemovableEnume rator a workable solution
where modifying IEnumerator would not be.


It could be good point. However I do not have enough information to make
final conclusion. I really don't know how IEnumerator is implemented - I
mean algorithms for HashTable, SortedList etc.

Anyway, I did not mean to add Remove method to interface itself. I meant
more like that specific implementation of IEnumerator (MoveNext, Current)
should react properly on Remove inside foreach. In the context of specific
collection resulting behavior depends on how internally enumeration
(MoveNext) and addition/removal are realized, right? How internal pointers
are managed, which information is kept where, which checks are performed
during each iteration etc. Just by looking at available methods it is clear,
that allowing Remove in foreach will not break IEnumerator - MoveNext,
Current and Reset are staying, you don't need anything extra. You need just
to change implementation - and not necessarily of interface, but maybe of
Remove/Add methods only.

I remember, when I had to write my own linked lists - one- or
bi-directional - Add and Remove, which could be called anytime, were always
priority One and they modified behavior of MoveNext/MovePrev and Current.
Because of this experience I do not understand, why it is so crucial to
prevent Remove to happen from inside foreach.

Rgds
Nov 15 '05 #7
inline
"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:O9******** ********@TK2MSF TNGP09.phx.gbl. ..
See inline
Consider if your removal code was to add or remove an object 10 or 20 units
behind your current position. If the code wasn't carefully written, then

the
index your accessing in your enumerator will skip or repeat a value and
probably cause trouble.


That's why I mentioned the problem with calling method of disposed/nulled
object - this also might happen after several lines or in another thread
etc. I don't think that language constructs might help with this kind of
errors.

languages constructs won't, quite agreeably, the language constructs Jon
proposed and the extensions i proposed are more for compiletype verification
and standarization, to avoid using Remove in situations where you shouldn't.
It should be noted that in most cases, using foreach, you don't access the
enumerator directly. I shoudl also note that my language construct
suggestion is the only one so far that would not result in a breaking
change, tho the syntax is horrid, ;).
However, another case comes about in Hashtables, SortedLists and the like, where there is no guarenteed or a dynamic order, adding or removing a item could throw off the enumeration in an unpredictable way, forcing the
IEnumerator writer to create a much more complex implementation, if they

can
support it at all.


I am not sure that collection.Add is really useful in foreach. Do you have
some specific example to discuss?
However even for Add for every specific collection there are logical
scenarios.
Remove is logical always for me. I haven't seen (yet) sound example, which
demonstrates unpredictabilit y here. That's why I pushed in the thread here
with my comments.

A foreach with add is less likely to be useful, mutability and removability
are far more valuable concepts so far. Addition is very useful in
illustrating possible problems, and i brought it in solely as an
illustration of some of the problems i could see. I think that the design
should be made to handle the most basic possiblities(ad ding, removing,
getting, and setting), and then culled down to what really is useful to a
programmer during dicussion(at this stage of discussion anyway, although we
havn't got a good reason, that doesn't mean someone won't think of a reason
that addition within the foreach would be useful after reading this thread)

Because of this, the IEnumerator interface was written to be as simple as possible and to cover only the very basic functionality. If Remove was

to be
added, i can almost guarentee it will result in a new interface, in part
because modifying IEnumerator would break a great deal of code, but also
because Remove would not work in all cases safetly. It would have to be
selectivly applied, that makes IRemovableEnume rator a workable solution
where modifying IEnumerator would not be.
It could be good point. However I do not have enough information to make
final conclusion. I really don't know how IEnumerator is implemented - I
mean algorithms for HashTable, SortedList etc.

Anyway, I did not mean to add Remove method to interface itself. I meant
more like that specific implementation of IEnumerator (MoveNext, Current)
should react properly on Remove inside foreach. In the context of specific
collection resulting behavior depends on how internally enumeration
(MoveNext) and addition/removal are realized, right? How internal pointers
are managed, which information is kept where, which checks are performed
during each iteration etc. Just by looking at available methods it is

clear, that allowing Remove in foreach will not break IEnumerator - MoveNext,
Current and Reset are staying, you don't need anything extra. You need just to change implementation - and not necessarily of interface, but maybe of
Remove/Add methods only.
Where would you propose placing the Remove/Add/Set methods?
Inside the IEnumerator interface(that would be breaking)
as an inheritor of IEnumerator (IDyanmicEnumer ator anyone?)
Remember, the code within the foreach must be capable of accessing the
Remove\Add\Set method easily, preferably with no complex casts or anything. I remember, when I had to write my own linked lists - one- or
bi-directional - Add and Remove, which could be called anytime, were always priority One and they modified behavior of MoveNext/MovePrev and Current.
Because of this experience I do not understand, why it is so crucial to
prevent Remove to happen from inside foreach.
Simple example:
Imagine if your linked list sorts objects based on size, on remove of an
object, the index in which the object is located causes everything ahead of
it for shift back, of course. But without knowing the size, it is more
difficult to determine where the shift will occur, the logic actually needs
to be inserted into the add\remove methods of the linked list itself. This
is just a case where some extended logic needs to be set, but it is
surmountable.
Also, should the object you jsut added to the head of the list, where you've
already been, be returned by foreach, or should it be dropped?

Complex Example:
You have a collection that stores objects in a tree, allowing for easy
location of a single object out of a large number. However it is a given
that all of your objects can be located in a linear manner (the objects are
always at the bottom level of a well balanced tree). Adding or removing a
node could cause a shift in the location of a great many nodes when the tree
is rebalanced, this would be a case where a change could be unpredictable.
This case too could be handled, tho it would involve alot of housekeeping
work to make sure there was no problems, probably an increase of 50-100
lines in your enumerator (and alot of practice to get it right), the node
shift code would have to do extra work to keep the enumerator up to date. It
could be very ugly in the end

Rgds

Nov 15 '05 #8
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
There would be two extra interfaces as well as IEnumerator:
IRemovableEnume rator and IMutableEnumera tor (or something like that).
The enumerator returned by an array would implement IMutableEnumera tor,


Why all these ?
To avoid the "for(int i=0;.." syntax ?
Write a VS macro that expands this syntax
I find that in most cases you need the "i" and rarely the
IRemovableEnume rator or IMutableEnumera tor
It can also move backwards<g>
which is the most common way to remove members from a list

I cannot understand this obsession (this is not directed to you only, but to
c# team also) to make foreach a super keyword (see iterators also),
since ever amateur developers do understand the "for(int i=0" syntax, i
wonder which is the target group of those "enhancemen ts" ?


Nov 15 '05 #9
"Eric Gunnerson [MS]" <er****@online. microsoft.com> wrote in message
news:eH******** ******@TK2MSFTN GP11.phx.gbl...
Jon,

You might be interested in this column I wrote a while back:

http://msdn.microsoft.com/library/de...rp01212002.asp

you could just use
foreach (string s in new SortedList (hash).Keys)
or
foreach (string s in new ArrayList(hash. Keys))

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1749
by: news | last post by:
I have a new situation I'm facing and could use a suggestion or two, as I don't seem to be able to think in the abstract very well. We have a local server which holds all of our image files. We have a remote server that runs our public Web server and mySQL database. I need to be able to run a script that will: Read the contents of a dir on the local server and a. make thumbnails of the files in it b. querey the database and pull...
1
1312
by: cnlai | last post by:
Hello ! I am distributing one application using MS Access. As for its security, I am thinking this way: 1. For end-users, they can interact with the application throght the custom menu items provided. They have no access to the tables directly. 2. A developer can have have full access to its tables for setting, correcting data.
7
1128
by: Homa | last post by:
Hi, I'm thinking what will happen if two users access a page at the same time. If there are any local variable in the page, will this cause concurrency problem? Simarily, if this page need to call some functions, say logic.doSomthing(), and the class logic is a singleton (for simplicity), will there be problems?
5
1087
by: Grant Merwitz | last post by:
I've seen many ads in these newgroups like the one posted about 4 below (Read Asp.NET in www.ebook5.com) Is this allowed? I find it highly annoying because people come here either to help, ask for help or learn from other problems/solutions. Not for SPAM! These cheap advertisers should F*** *FF
7
1267
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a variable declaration What is the difference between the two (I strongly suspect there is none)
0
921
by: staeri | last post by:
I have a datalist which shows accounts and the forecasted amount for each account. The forecasted amount is entered into textboxes by the user. Now I want to add functionality so that the user can be able to specify which months the forecasted amount is likely to occur for each account and I'm thinking about the best way of doing this. That means the user can enter a forecast of 100 on account 3010. He can stay with that the way the...
10
1920
by: vectorBS | last post by:
Hi, I need a suggestion as to which method will be most efficient way of accomplishing this task. I need to build invoice report with Garment Sizes and Quantity. There are seperate records for different sizes with their corresponding quantity in the shipment table. I have to display this information in Invoice Report in a single row with quntities under size lables ( S, M, L, XL, XXL ). Access doesnt allow grouping on multiple fields so i...
24
2098
by: Mike Hofer | last post by:
Please forgive the cross-post to multiple forums. I did it intentionally, but I *think* it was appropriate given the nature of my question. I'm working on an open source code library to help automate and clean up parameter validation code. It's almost ready to go into open beta. But one last little glitch is holding me up, and that would be the name of the factory class that serves as the entry point into the library: Validate.
15
1156
by: dhtml | last post by:
Tried mailing the FAQ maintainer but the mail bounced. The FAQ entry of topic: http://www.jibbering.com/faq/#FAQ4_15 DocDom = (document.getElementById?true:false); DocAll = (document.all?true:false); DocStr='' if (DocAll) DocStr="return document.all" if (DocDom) DocStr="return document.getElementById(id)"
0
8380
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.