M
Michael Starberg
... and it is only January. This might get worse. =)
I really like languages like C#, that has a foreach on IEnumerable<T>.
But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:
for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }
I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.
I don't think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.
So I tried creating extentions on IList<T>
Simple function that simply returns Count-1
First up was UpperBound()
for (int i = 0; i < theList.UpperBound() ; i++) { [etc.. etc..] }
That made it look way tooo much like Visual Basic for me, so no thanks.
In Delphi/Pascal you have pred() and succ() on ordinal types, so you could
(and should) do:
for i := 0 to pred(theList.Count) do begin [etc.. etc..] end;
I tried that:
for (int i = 0; i < theList.PredCount() ; i++) { [etc.. etc..] }
That made no sense at all, so I tried end:
for (int i = 0; i < theList.End() ; i++) { [etc.. etc..] }
I've tried several combinations of stop and end words and the best I can
come with is .ToEnd()
for (int i = 0; i < theList.ToEnd() ; i++) { [etc.. etc..] }
Any thoughts, except that I should ToEnd even thinking of this? =)
- Michael Starberg
I really like languages like C#, that has a foreach on IEnumerable<T>.
But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:
for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }
I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.
I don't think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.
So I tried creating extentions on IList<T>
Simple function that simply returns Count-1
First up was UpperBound()
for (int i = 0; i < theList.UpperBound() ; i++) { [etc.. etc..] }
That made it look way tooo much like Visual Basic for me, so no thanks.
In Delphi/Pascal you have pred() and succ() on ordinal types, so you could
(and should) do:
for i := 0 to pred(theList.Count) do begin [etc.. etc..] end;
I tried that:
for (int i = 0; i < theList.PredCount() ; i++) { [etc.. etc..] }
That made no sense at all, so I tried end:
for (int i = 0; i < theList.End() ; i++) { [etc.. etc..] }
I've tried several combinations of stop and end words and the best I can
come with is .ToEnd()
for (int i = 0; i < theList.ToEnd() ; i++) { [etc.. etc..] }
Any thoughts, except that I should ToEnd even thinking of this? =)
- Michael Starberg