"C# for the Completely Uninitiated: Tutorial #8 Source Code Listings"

C# for the Completely Uninitiated

Tutorial #8: Source Code Listings

Back to Tutorial #8


Program namesarray.cs

Demonstrates use of foreach loop to traverse an array


using System;

class Program {
    
    static void Main(string[] args) {

        string[] names = {"Zoey","Bryan","Beth","Carl","Glenda","Eric",
                "Frank","Gary","David","Hubert","Helga","Mike",
                "Sarah","Allan"};

        foreach (string name in names) {

            Console.WriteLine("\t{0}", name);

        }

        Console.Write("\n\tTo exit program, press the Enter key... ");
        Console.ReadLine();
    }

}




Program boundslength.cs

Demonstrates use of the Length property and the bounds-getting methods of an array



using System;

class Program {
    
    static void Main(string[] args) {

        string[] strArray = { "str1", "str2", "str3", "str4", "str5" };
        int lower = strArray.GetLowerBound(0);
        int upper = strArray.GetUpperBound(0);
        int length = strArray.Length;

        Console.WriteLine("\n\tstring[] strArray = {\"str1\",\"str2\",\"str3\",\"str4\",\"str5\"};\n");

        Console.WriteLine("\tLowerBound = {0}   //strArray[{1}] holds the value \"{2}\"\n",lower,lower,strArray[lower]);
        
        Console.WriteLine("\tUpperBound = {0}   //strArray[{1}] holds the value \"{2}\"\n", upper, upper, strArray[upper]);
        
        Console.WriteLine("\tArray length = {0}    // i.e., there are five total elements (elements 0,1,2,3, and 4)\n", length);

        Console.Write("\tTo exit program, press the Enter key... ");
        Console.ReadLine();
    }

}




Program deptstore.cs

Demonstrates use of a two-dimensional array



using System;

namespace DepartmentStore
{
    class Program
    {
        static void Main(string[] args)
        {

            // The first list in ItemNumber contains women's items
            // The other contains non-women items

            long[,] ItemNumber = new long[2, 3]{
                  { 947783, 934687, 973947 },
                  { 739579, 367583, 743937 }
               };

            string[,] ItemName = new string[2, 3]
	    {
		{
		    "Women Double-faced wool coat",
		    "Women Floral Silk Tank Blouse",
                    "Women Push Up Bra"
		},

		{
		    "Men Cotton Polo Shirt",
		    "Children Cable-knit Sweater  ",
                    "Children Bear Coverall Cotton"
		}
	    };
        
            decimal[,] UnitPrice = new decimal[2, 3]
	    {
                { 275.25M, 180.05M, 50.00M },
                { 45.55M, 25.65M, 28.25M }
	    };
		

            for(int i=0; i<2; i++){

                for(int j=0; j<3; j++){

                    Console.WriteLine("Receipt:");
                    Console.WriteLine("\tItem Number: {0}", ItemNumber[i,j].ToString());
                    Console.WriteLine("\tDescription: {0}", ItemName[i,j]);
                    Console.WriteLine("\tUnit Price:  {0:C}\n", UnitPrice[i,j].ToString());                        

                }

            }

            Console.Write("\n\tPress the Enter key to quit program... ");
            Console.ReadLine();


        }
    }
}




Program jobcandidates.cs

Demonstrates use of a two-dimensional array




using System;

class Program
{
    static void Main()
    {

        string[,] candidates = {
                {"Mr. Johnson", "resume #1", "personal reference #1"},
                {"Ms. Burton", "resume #2", "personal reference #2"},
                {"Ms. Smith", "resume #3", "personal reference #3"}
        };

        Console.WriteLine();

        for(int i=0; i<3; i++){

                for(int j=0; j<3; j++){

                        Console.WriteLine("\t{0}",candidates[i,j]);

                }//end nested loop

                Console.WriteLine();
        }//end outer loop

	Console.Write("Press the Enter key to end program... ");
        Console.ReadLine();


    }
}




Program simplenested.cs

Demonstrates use of a nested for loop



using System;

class Program
{
    static void Main()
    {


        Console.WriteLine("\n\tThis program demonstrates the following nested for loop:\n");

        string s = "for(int i=1; i<4; i++){\n";
        s += "\t\tfor(int j=1; j<4; j++){\n";
        s += "\t\tConsole.Write(\"\\t{0} + {1} = {2}  // ";
        s += "outer loop i = {0}, nested loop j = {1}\",";
        s += "i,j,i+j);\n";
        s += "\t\t}\n";
        s += "\t}";

        Console.WriteLine("\t{0}\n",s);

        Console.WriteLine("\tAnd here is the output produced by the nested for loop shown above:\n");


        for(int i=1; i<4; i++){

            for(int j=1; j<4; j++){
                 Console.WriteLine("\t{0} + {1} = {2}  // outer loop i = {0}, nested loop j = {1}", i, j, i + j);
            }
        }


        Console.WriteLine();

        Console.WriteLine("\tAs you can see, for each iteration of the outer loop, the nested loop repeats three times\n");

        Console.Write("\tPress the Enter key to end program... ");
        Console.ReadLine();

    }
}




Program threed.cs

Demonstrates a three-dimensional array



using System;

class Program {

    static void Main() {

        int[, ,] threeD = new int[2, 3, 4]{
            
            {{0,1,2,3},{4,5,6,7},{8,9,10,11}},
            {{12,13,14,15},{16,17,18,19},{20,21,22,23}}

        };

        for (int i = 0; i < 2; i++) {

            for (int j = 0; j < 3; j++) {

                for (int k = 0; k < 4; k++) {

                    Console.WriteLine(threeD[i,j,k]);

                }

            }

        }

        Console.WriteLine("Press the Enter key to exit program... ");
        Console.ReadLine();
    }

}