8.3. Array Methods

As with strings, C# provides us with useful methods for arrays. These methods will either alter an existing array, return information about the array, or create and return a new array.

8.3.1. Common Array Methods

Here is a sample of the most frequently used array methods. More complete lists can be found here:

  1. Microsoft Array Methods

  2. Geeks for Geeks Array Methods

To see detailed examples for a particular method, control-click (or right-click) on its name.

Methods That Return Information About The Array

Method

Syntax

Description

IndexOf

Array.IndexOf(arrayName, item)

Returns the index of the FIRST occurrence of an item in the array.

LastIndexOf

Array.LastIndexOf(arrayName, item)

Returns the index of the LAST occurrence of an item in the array. If the item is not in the array, -1 is returned.

GetValue

arrayName.GetValue(index)

Returns the value of specified index.

Methods That Rearrange The Entries In The Array

Method

Syntax

Description

Reverse

Array.Reverse(arrayName)

Reverses the order of the elements in an array.

Sort

Array.Sort(arrayName)

Arranges the elements of an array into increasing order.

Methods That Add Or Remove Values From An Array

Method

Syntax

Description

Resize

Array.Resize(ref arrayName, desiredLength)

Adds of Removes indicies based on desiredLength value.

Methods That Update Values Within An Array

Method

Syntax

Description

SetValue

arrayName.SetValue(new item, index)

Updates index value with new item.

Copy

Array.Copy(originalArray, copiedArray, numItems)

Updates index value with new item.

String Methods That Update Array Values

Method

Syntax

Description

Join

string.Join("connecter", arrayName)

Combines all the elements of an array into a string.

Split

stringName.Split("delimiter")

Divides a string into smaller string pieces, which are stored in a new array.

ToCharArray

stringName.ToCharArray()

Divides a string into chars, which are stored in a new array.

8.3.2. Check Your Understanding

Follow the links in the table above for the Sort, Reverse, Split and Join methods. Review the content and then answer the following questions.

Question

What is printed by the following code?

1
2
3
4
5
6
7
string[] charles = {"coder", "Tech", "47", "23", "350"};
Array.Sort(charles);
Console.WriteLine(charles[0]);
Console.WriteLine(charles[1]);
Console.WriteLine(charles[2]);
Console.WriteLine(charles[3]);
Console.WriteLine(charles[4]);
  1. "350", "23", "47", "Tech", "coder"

  2. "coder", "Tech", "23", "47", "350"

  3. "23", "47", "350", "Tech", "coder"

  4. "23", "350", "47", "coder", "Tech"

Question

Which statement converts the string string phrase = "LaunchCode students rock!" into the array string[] words = {"LaunchCode", "students", "rock!"}?

  1. string.Join(" ", phrase);

  2. phrase.Split(" ");

  3. string.Join("", phrase);

  4. phrase.Split("");

Question

What is printed by the following program?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
string[] groceryBag = {"bananas", "apples", "edamame", "chips", "cucumbers", "milk", "cheese"};
string[] selectedItems = new string[4];

Array.Copy(groceryBag, selectedItems, 4);
Array.Sort(selectedItems);

Console.WriteLine(selectedItems[0]);
Console.WriteLine(selectedItems[1]);
Console.WriteLine(selectedItems[2]);
Console.WriteLine(selectedItems[3]);
  1. "apples", "bananas", "edamame", "chips"

  2. "chips", "cucumbers", "edamame", "milk"

  3. "apples", "banana", "chips", "edamame"

  4. "cheese", "chips", "cucumbers", "edamame"