Arrays as Function Returns and Parameters : Function Parameters « Language Basics « C# / C Sharp






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);
    }
}

 








Related examples in the same category

1.Reference, output and value parameters.
2.Passing parameters to methods
3.creates instances of a value and a reference type
4.Pass valuel by pointer
5.ref pointer parameter
6.Use ref to mark an object parameter
7.Use out to mark an object parameter
8.Pass integer by reference