Abstract Classes and Methods

   
 


using System;

abstract public class MotorVehicle {
    public string make;
    public string model;

    public MotorVehicle(string make, string model) {
        this.make = make;
        this.model = model;
    }
    abstract public void Accelerate();

}

public class Product : MotorVehicle {
    public Product(string make, string model) :
        base(make, model) {
        // do nothing
    }

    public override void Accelerate() {
        Console.WriteLine("In Product Accelerate() method");
        Console.WriteLine(model + " accelerating");
    }
}


class MainClass {
    public static void Main() {
        Product myProduct = new Product("Toyota", "MR2");
        myProduct.Accelerate();
    }
}

    


Simplest while

   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_4___Program_Control
{
  public class Whiles
  {
    static void Main(string[] args)
    {
      int a = 0;
      
      while(a > 10)
      {
        a++;
        Console.WriteLine(a);
      }
    }
  }
}

           
          


Simplest do while

/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;

namespace Client.Chapter_4___Program_Control
{
public class DoWhile
{
static void Main(string[] args)
{
int a = 0;

do
{
a++;
Console.WriteLine(a);
} while (a < 10); } } } [/csharp]

While true test


   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;
 public class WhileTrueTester
 {
     public static void Main()
     {
         int counterVariable = 0;  // initialization

         while (true)
         {
             Console.WriteLine(
                 "counter: {0} ", counterVariable++); // increment

             if (counterVariable > 10) // test
                 break;
         }
     }
 }

           
          


a do…while loop


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example4_10.cs illustrates the use of
  a do...while loop
*/

public class Example4_10
{

  public static void Main()
  {

    int counter = 1;
    do
    {
      System.Console.WriteLine("counter = " + counter);
      counter--;
    }
    while (counter > 1);

  }

}


           
          


while loop to calculate and display the Fibonacci numbers less than 50

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_9.cs illustrates the use of
a while loop to calculate and display
the Fibonacci numbers less than 50
*/

public class Example4_9
{

public static void Main()
{

// initialize the first two numbers in the sequence
int oldNumber = 1;
int currentNumber = 1;

int nextNumber;

System.Console.Write(currentNumber + ” “);

while (currentNumber < 50) { System.Console.Write(currentNumber + " "); // calculate the next number by adding the // current number to the old number nextNumber = currentNumber + oldNumber; oldNumber = currentNumber; currentNumber = nextNumber; } } } [/csharp]

While loop to display 1 to 5

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example4_8.cs illustrates the use of
a while loop to display 1 to 5
*/

public class Example4_8
{

public static void Main()
{

int counter = 1;
while (counter <= 5) { System.Console.WriteLine("counter = " + counter); counter++; } } } [/csharp]