How to create an infinite loop

Obvious in retrospect, but took me a minute to catch on to what’s going on here:

public void DoStuff(byte maximum)
{
    for(byte i = 1; i <= maximum; i++)
    {
      // whatever
    }
}

This works like a trooper until someone passes in a byte.MaxValue in a unit test and i becomes 0 after reaching maximum and getting incremented again.

Advertisement
Previous Post
Leave a comment

3 Comments

  1. Could you imagine finding that as a defect down the road…. Thank God for Unit Tests!

    Reply
  2. Interesting bug, glad it got caught. Looks inconspicuous enough.

    Reply
  3. This is where the checked keyword can save your bacon.

    Reply

Leave a Reply to Joshua Kehn Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: