12
Answers

NP87 foreach

Hi Guys

 

NP87 foreach

 

foreach is used when checks size of the array. In this program it is used without array. Anyone knows please explain the reason.

    

Cars carLot = new Cars();

 

foreach (Car c in carLot)

{

 …………………          

}

 

Thank you

 

 

using System;

using System.Collections;

 

namespace ObjEnum

{

   public class Car

   {

      // Internal state data.

      private int currSpeed;

      private int maxSpeed;

      private string petName;

 

      // Is the car alive or dead?

      bool carIsDead;

 

      // A car has-a radio.

      private Radio theMusicBox = new Radio();

 

      public Car()

      {

         maxSpeed = 100;

      }

 

      public Car(string name, int max, int curr)

      {

         currSpeed = curr;

         maxSpeed = max;

         petName = name;

      }

 

      public void CrankTunes(bool state)

      {

         // Tell the radio play (or not).

         // Delegate request to inner object.

         theMusicBox.TurnOn(state);

      }

 

      public void SpeedUp(int delta)

      {

         // If the car is dead, just say so...

         if (carIsDead)

         {

            Console.WriteLine("{0} is out of order....", petName);

         }

         else     // Not dead, speed up.

         {

            currSpeed += delta;

            if (currSpeed >= maxSpeed)

            {

               Console.WriteLine("{0} has overheated...", petName);

               carIsDead = true;

            }

            else

               Console.WriteLine("\tCurrSpeed = {0}", currSpeed);

         }

      }

 

      // Properties.

      public string PetName

      {

         get { return petName; }

         set { petName = value; }

      }

      public int CurrSpeed

      {

         get { return currSpeed; }

         set { currSpeed = value; }

      }

      public int MaxSpeed

      {

         get { return maxSpeed; }

         set { maxSpeed = value; }

      }

   }

 

   // No pun intended!

   public class CarDriver

   {

      public static void Main()

      {

         Cars carLot = new Cars();

 

         Console.WriteLine("***** Here are the cars in your lot *****");

         foreach (Car c in carLot)

         {

            Console.WriteLine("-> Name: {0}", c.PetName);

            Console.WriteLine("-> Max speed: {0}", c.MaxSpeed);

            Console.WriteLine();

         }

 

         #region Working with raw IEnumerator

         /*

                  // Now ala IEnumerator

                  IEnumerator itfEnum;

                  itfEnum = (IEnumerator)carLot;

 

                  // Reset the cursor to the beginning.

                  itfEnum.Reset();

 

                  // Advance internal cursor by 1.

                  itfEnum.MoveNext();

           

                  // Cast to a Car and crank some tunes.

                  object curCar = itfEnum.Current;

                  ((Car)curCar).CrankTunes(true);

                  */

         #endregion

      }

   }

 

   public class Cars : IEnumerable // , IEnumerator

   {

      // This class maintains an array of cars.

      private Car[] carArray;

 

      // Current position in array.

      // int pos = -1;

 

      public Cars()

      {

         carArray = new Car[4];

         carArray[0] = new Car("FeeFee", 200, 0);

         carArray[1] = new Car("Clunker", 90, 0);

         carArray[2] = new Car("Zippy", 30, 0);

         carArray[3] = new Car("Fred", 30, 0);

      }

 

      #region Raw IEnumerator impl

      /*

            // Implementation of IEnumerator.

            public bool MoveNext()

            {

                  if(pos < carArray.Length)

                  {

                        pos++;

                        return true;

                  }

                  else

                        return false;

            }

            public void Reset()

            {

                  pos = 0;

            }

            public object Current

            {

                  get { return carArray[pos]; }

            }

            */

      #endregion

 

      // This must be present in order to let the foreach

      // expression to iterate over our array.

      // IEnumerable implemtation.

      public IEnumerator GetEnumerator()

      {

         // return (IEnumerator)this;

 

         // Now just get back the IEnumerator from

         // the internal array!

         return carArray.GetEnumerator();

      }

   }

 

   public class Radio

   {

      public void TurnOn(bool on)

      {

         if (on)

            Console.WriteLine("Jamming...");

         else

            Console.WriteLine("Quiet time...");

      }

   }

}

/*

***** Here are the cars in your lot *****

-> Name: FeeFee

-> Max speed: 200

 

-> Name: Clunker

-> Max speed: 90

 

-> Name: Zippy

-> Max speed: 30

 

-> Name: Fred

-> Max speed: 30

*/

 

 

Answers (12)