Test Your Knowledge: Exercises

Exercise 9-1. You’ll use the following program for this exercise. Either type it into Visual Studio, or copy it from this book’s website. Note that this is spaghetti code—you’d never write method calls like this, but that’s why this is the debugging chapter.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise_9_1
{
     class Tester
    {
        public void Run( )
        {
            int myInt = 42;
            float myFloat = 9.685f;
            Console.WriteLine("Before starting: \n value of myInt: 
                    {0} \n value of myFloat: {1}", myInt, myFloat);
            // pass the variables by reference
            Multiply( ref myInt, ref myFloat );
            Console.WriteLine("After finishing: \n value of myInt: 
                    {0} \n value of myFloat: {1}", myInt, myFloat);
         }
         private static void Multiply (ref int theInt, 
                                       ref float theFloat)
         {
            theInt = theInt * 2;
            theFloat = theFloat *2;
            Divide( ref theInt, ref theFloat);
         }
         private static void Divide (ref int theInt, 
                                     ref float theFloat)
         {
            theInt = theInt / 3;
            theFloat = theFloat / 3;
            Add(ref theInt, ref theFloat);
         }
         public static void Add(ref int theInt, 
                                ref float theFloat)
         {
            theInt = theInt + theInt;
            theFloat = theFloat + theFloat;
         }
         static void Main( )
         {
            Tester t = new Tester( );
            t.Run( );
         }
    }
}
  1. Place a breakpoint in Run( ) on the following line, and then run the program:

    Console.WriteLine("Before starting: \n value of myInt:
                 {0} \n value of myFloat: {1}", myInt, myFloat);

    What are the values of myInt and myFloat at the breakpoint?

  2. Step into the Multiply( ) method, ...

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.