Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C#:
- Part #1:
- string[] items = {"a","b","c","d"};
- int i = 0;
- foreach (string item in items)
- {
- Console.WriteLine(++i);
- }
- Console.WriteLine("");
- i = 0;
- foreach (string item in items)
- {
- Console.WriteLine(i++);
- }
- Output:
- 1
- 2
- 3
- 4
- 0
- 1
- 2
- 3
- Part #2:
- Pre-increment ++i increments the value of i and evaluates to the new incremented value.
- int i = 3;
- int preIncrementResult = ++i;
- Assert( preIncrementResult == 4 );
- Assert( i == 4 );
- Post-increment i++ increments the value of i and evaluates to the original non-incremented value.
- int i = 3;
- int postIncrementResult = i++;
- Assert( postIncrementtResult == 3 );
- Assert( i == 4 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement