Side-effects can be important

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Side-effects can be important.

using System;

public class SideEffects {
public static void Main() {
int i;

i = 0;

/* Here, i is still incremented even though
the if statement fails. */
if(false & (++i < 100)) Console.WriteLine("this won't be displayed"); Console.WriteLine("if statement executed: " + i); // displays 1 /* In this case, i is not incremented because the short-circuit operator skips the increment. */ if(false && (++i < 100)) Console.WriteLine("this won't be displayed"); Console.WriteLine("if statement executed: " + i); // still 1 !! } } [/csharp]