A jagged array can also contain multidimensional arrays. For example, the following declaration declares nums to be a jagged array with each element pointing to a two-dimensional array:
int[][,] nums = new int[][,] {
new int[,] {{ 1, 2 }, { 3, 4 }},
new int[,] {{ 5, 6 }, { 7, 8 }}
};
To access an individual element within the jagged array, you can use the following statements:
Console.WriteLine(nums[0][0, 0]); //---1---
Console.WriteLine(nums[0][0, 1]); //---2---
Console.WriteLine(nums[0][1, 0]); //---3---
Console.WriteLine(nums[0][1, 1]); //---4---
Console.WriteLine(nums[1][0, 0]); //---5---
Console.WriteLine(nums[1][0, 1]); //---6---
Console.WriteLine(nums[1][1, 0]); //---7---
Console.WriteLine(nums[1][1, 1]); //---8---
Used on a jagged array, the Length
property of the Array abstract base class returns the number of arrays contained in the jagged array:
Console.WriteLine(nums.Length); //---2---
Parameter Arrays
In C#, you can pass variable numbers of parameters into a function/method using a feature known as
string firstName = 'Wei-Meng';
string lastName = 'Lee';
Console.WriteLine('Hello, {0}', firstName);
Console.WriteLine('Hello, {0} {1}', firstName, lastName);
Observe that the last two statements contain different numbers of parameters. In fact, the WriteLine()
method is overloaded, and one of the overloaded methods has a parameter of type params
(see Figure 13-3). The params
keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

Figure 13-3
A result of declaring the parameter type to be of params is that callers to the method do not need to explicitly create an array to pass into the method. Instead, they can simply pass in a variable number of parameters.
To use the params
type in your own function, you define a parameter with the params
keyword:
private void PrintMessage(string prefix, params string[] msg) {
}
To extract the parameter array passed in by the caller, treat the params parameter like a normal array, like this:
private void PrintMessage(string prefix, params string[] msg) {
foreach (string s in msg)
Console.WriteLine('{0}>{1}', prefix, s);
}
When calling the PrintMessage()
function, you can pass in a variable number of parameters:
PrintMessage('C# Part 1', 'Arrays', 'Index', 'Collections');
PrintMessage('C# Part 2', 'Objects', 'Classes');
These statements generate the following output:
C# Part 1>Arrays
C# Part 1>Index
C# Part 1>Collections
C# Part 2>Objects
C# Part 2>Classes
A params
parameter must always be the last parameter defined in a method declaration.
Copying Arrays
To copy from one array to another, use the Copy()
method from the Array
abstract base class:
int[] num = new int[5] { 1, 2, 3, 4, 5 };
int[] num1 = new int[5];
num.CopyTo(num1, 0);
These statements copy all the elements from the num
array into the num1
array. The second parameter in the CopyTo()
method specifies the index in the array at which the copying begins.
Collections Interfaces
The System.Collections
namespace contains several interfaces that define basic collection functionalities:
The interfaces described in the following list are the generic versions of the respective interfaces. Beginning with C# 2.0, you should always try to use the generic versions of the interfaces for type safety. Chapter 9 discusses the use of generics in the C# language.
Interface | Description |
---|---|
IEnumerable<T> and IEnumerator<A> | Enable you to loop through the elements in a collection. |
ICollection<T> | Contains items in a collection and provides the functionality to copy elements to an array. |