Goto Tester

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class GotoTester
{

public static void Main()
{
int counterVariable = 0;

repeat: // the label

Console.WriteLine(
“counterVariable: {0}”,counterVariable);

// increment the counter
counterVariable++;

if (counterVariable < 10) goto repeat; // the dastardly deed } } [/csharp]

Demonstrate the goto

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the goto.

using System;

public class Use_goto {
public static void Main() {
int i=0, j=0, k=0;

for(i=0; i < 10; i++) { for(j=0; j < 10; j++ ) { for(k=0; k < 10; k++) { Console.WriteLine("i, j, k: " + i + " " + j + " " + k); if(k == 3) goto stop; } } } stop: Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k); } } [/csharp]

Use goto with a switch

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use goto with a switch.

using System;

public class SwitchGoto {
public static void Main() {

for(int i=1; i < 5; i++) { switch(i) { case 1: Console.WriteLine("In case 1"); goto case 3; case 2: Console.WriteLine("In case 2"); goto case 1; case 3: Console.WriteLine("In case 3"); goto default; default: Console.WriteLine("In default"); break; } Console.WriteLine(); } // goto case 1; // Error! Can't jump into a switch. } } [/csharp]

Arrays as Function Returns and Parameters

   
 
using System;

public class Starter {

    public static void Main() {
        int[] zArray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
        string[] xArray = { "a", "b", "c", "d" };
        Console.WriteLine("List Numbers");
        MyClass.ListArray(zArray);
        Console.WriteLine("List Letters");
        MyClass.ListArray(xArray);
        Console.WriteLine("Total Numbers");
        MyClass.Total(zArray);
    }
}


public class MyClass {

    public static void ListArray(Array a) {
        foreach (object element in a) {
            Console.WriteLine(element);
        }
    }

    public static void Total(int[] iArray) {
        int total = 0;
        foreach (int number in iArray) {
            total += number;
        }
        Console.WriteLine(total);
    }
}