Passing an Array to a Method

A variable that is the name of an array is just a reference to an instance of System.Array.  Since it is a reference type, when you pass an array to a method, you pass a copy of the reference.  This means that the method has the ability to read and write elements of the array.
Here’s a method that accepts an array as an input parameter.  The code demonstrates that you can read from and write to elements of the array.
1
2
3
4
5
6
7
8
9
10
11
12
public static void DumpMovieInfo(MovieInfo[] movieList)
{
    for (int i = 0; i < movieList.Length; i++)
    {
        // Read element of the array
        Console.WriteLine("{0} ({1}), {2}", movieList[i].Title, movieList[i].Year, movieList[i].Director);
 
        // Rewrite element
        movieList[i].Title = movieList[i].Title.ToUpper();
        movieList[i].Director = movieList[i].Director.ToUpper();
    }
}
You would call the method as follows:
1
2
3
4
5
6
7
MovieInfo[] movies = { new MovieInfo("The African Queen", 1951, "John Huston"),
                       new MovieInfo("Casablanca", 1942, "Michael Curtiz"),
                       new MovieInfo("The Godfather", 1972, "Francis Ford Coppola")};
 
Movie.DumpMovieInfo(movies);
 
Movie.DumpMovieInfo(movies);