Category Archives: arrays

Convert between ragged arrays and two-dimensional arrays

Ragged arrays are arrays that hold other arrays, as opposed to two-dimensional arrays. They are called “ragged arrays” because they work like two-dimensional arrays (or higher-dimensional arrays) where each row in the array can hold a different number of elements. … Continue reading

Posted in algorithms, arrays, extension methods, generic | Tagged , , , , , , , , , , , | Leave a comment

Copy ListView data into an array in C#

This example uses the following ListView extension method to copy ListView data into a two-dimensional array of strings. // Return the ListBox’s contents in a string[,]. public static string[,] GetListViewData(this ListView lvw) { // Get the number of rows and … Continue reading

Posted in arrays, controls, strings | Tagged , , , , , , , , , , | Leave a comment

Make a Shakespeare insult generator in C#

This example shows how to build a Shakespeare insult generator by selecting one word from each of three arrays and combining the results. The following code shows the arrays (with many values omitted to save space). // The insult pieces … Continue reading

Posted in arrays, miscellany, strings | Tagged , , , , , , , , , , , , , , | Leave a comment

Make extension methods that pick random items from arrays or lists in C#

This example uses a simple but useful technique to let you pick random items from arrays and lists. The following code defines RandomElement extension methods that pick random items. public static class ArrayExtensions { // The random number generator. private … Continue reading

Posted in arrays, extension methods, lists | Tagged , , , , , , , , , | 1 Comment

Cast arrays from one reference type to another in C#

Every C# programmer knows that you can use a cast operator to convert one data type into another. For example, if the Employee class inherits from the Person class, then the following code creates an Employee object and then makes … Continue reading

Posted in arrays, syntax, variables | Tagged , , , , , , , , , , , , , | Leave a comment

Make subarray extension methods in C#

The Array class’s Copy method can copy a subarray from one array to another, but it requires that you figure out how many items you want to copy. It will also copy parts of a two-dimensional array, but it only … Continue reading

Posted in arrays, extension methods | Tagged , , , , , , , , , | Leave a comment

Use Array.Copy to copy values in two-dimensional arrays in C#

The Array.Copy method can copy values from one array to another. For one-dimensional arrays, this is reasonably straightforward. You specify the source and destination arrays, the indices where copying should start in the two arrays, and the number of items … Continue reading

Posted in arrays | Tagged , , , , , , , , | Leave a comment

Iterate over items in an array with unknown dimensions in C#

This example shows how you can iterate over the items in an array that has an unknown number of dimensions. When it starts, the program executes the following code. private void Form1_Load(object sender, EventArgs e) { string[, ,] values = … Continue reading

Posted in algorithms, arrays, syntax | Tagged , , , , , , , , , , , , , | 2 Comments

Solution to puzzle: Zero rows and columns in an array in C#

This post gives four solutions to Puzzle: Zero rows and columns in an array in C#. If you want to try the puzzle for yourself, see that post before you read this one. This is a fairly long post, but … Continue reading

Posted in arrays, games | Tagged , , , , , , , , , | 3 Comments

Use Array methods in C#

The example Sort and search arrays in C# shows how to use two useful Array methods: Sort and BinarySearch. This example demonstrates some other useful Array methods. For simplicity, this example uses arrays of characters to demonstrate the Array methods, … Continue reading

Posted in arrays, classes | Tagged , , , , , , , , , , , , , | Leave a comment