for(10)
- Is there an easy and clear way to loop a set number of times in c#? How close can I get to something like : do(5) or for(5)?
I find that every time I write the following I have to do a double take and make sure the loop is going to iterate the number of times I want it to:
for( int cx = 0 ; cx < 5 ; ++cx )
I wrote a class called "Counter" that enables me to "for" a loop the number of times specified in the constructor. I like that I can think less when coding, but it is still too much typing for my taste.
int loopCx = 0;
for (var cx = new Counter(5); cx.Next(); )
{
loopCx += 1;
}
MessageBox.Show("loop count: " + loopCx);
public class Counter { int mCurrent; int mLimit ; public Counter(int InLimit) { mCurrent = 0; mLimit = InLimit; } public int Current { get { return mCurrent; } set { mCurrent = value; } } public bool Next() { if (mCurrent >= mLimit) return false; else { mCurrent += 1; return true; } }
Answers
- for (int i = 0; i < 5; i++)
{
// repeat 5 times.
}
That's about as small as you can get.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byJi.ZhouMSFT, ModeratorTuesday, November 10, 2009 2:12 AM
- Proposed As Answer byReed Copsey, Jr. Tuesday, November 03, 2009 4:41 PM
All Replies
- for (int i = 0; i < 5; i++)
{
// repeat 5 times.
}
That's about as small as you can get.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byJi.ZhouMSFT, ModeratorTuesday, November 10, 2009 2:12 AM
- Proposed As Answer byReed Copsey, Jr. Tuesday, November 03, 2009 4:41 PM
- I completely agree with David here - just use a standard for loop. It's much, much easier to understand, and shorter than what you have.
Theoretically, in C#3, you can shave a few characters of typing off what he put (15 vs. 21 in the simplest for loop), by making a static class that takes a lambda. I wouldn't ever suggest doing this, but I think the shortest loop construct that still provides a "current" value would be:
However, I highly recommend not trying to do something like this. If I ever ran across code with something this crazy in it, I'd run away, as fast and as far as possible ;)public static class C { public static void F(int value, Action<int> action) { for (int i = 0; i < value; ++i) action(i); } } class Program { static void Main(string[] args) { C.F(3,(i)=>{ Console.WriteLine("Current index: {0}",i); }); Console.ReadKey(); } }
Reed Copsey, Jr. - http://reedcopsey.com - ok. thanks for the replies. My solution is to be able to design and embed your own DSL like code in the larger program. Not sure how that would all work, but it would be neat to be able to embed XAML style markup within C# code.
ok. thanks for the replies. My solution is to be able to design and embed your own DSL like code in the larger program. Not sure how that would all work, but it would be neat to be able to embed XAML style markup within C# code.
That's a mistake. Be sure to document it so that everyone who doesn't get it understands what you're trying to do. They'll all get the "for" loop, but custom stuff requires explanation.
I'd learn the language fully before you start embedding changes. Trust me, I've been exactly where you are now. You're going to regret the decision to try to make C# more declarative. It's not at declarative language by default. If you're looking for something more declarative, check out F#. I'm a huge fan of what they've done, and it might be more up your alley.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowserfor (int i = 0; i++ < 5; ) { //repeat five times }for (int i = 0; i++ < 5; ) { //repeat five times }
Copycat. :)
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Another option for you:
static IEnumerable<int> Do( int n ) { return Enumerable.Range( 0, n ); } static void Main( string[] args ) { foreach(var i in Do(5)) { } }


