Category Archives: arrays

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

Puzzle: Zero rows and columns in an array in C#

This puzzle is given as a coding example in the book Cracking the Coding Interview by Gayle Laakmann McDowell. The puzzle is: Write an algorithm such that if an element in an M x N matrix is 0, its entire … Continue reading

Posted in arrays, games | Tagged , , , , , , , , | 1 Comment

Iterate over the items in arrays and lists in C#

You can use a foreach loop to iterate over the items in arrays, lists, collections, and other objects that have a GetEnumerator method. These loops are often more convenient than for or while loops because you don’t need to worry … Continue reading

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

Give a class default indexer properties in C#

An indexer property is a property that other code can use inside square brackets to access some value provided by the class, much as you can use the index of an array. Unlike an array, a class’s indexer need not … Continue reading

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

Loop over an array of unknown dimension in C#

The previous post Loop over entries in an array of unknown length in C# explains how to loop over the entries in a one- or two-dimensional array, but what do you do if you don’t know how many dimensions an … Continue reading

Posted in algorithms, arrays, recursion, variables | Tagged , , , , , , , , , , , , , , , , | 1 Comment

Loop over entries in an array of unknown length in C#

One way to loop through the items in an array is to hard code its length into the program. Alternatively you can use the array’s Length property to determine how many items it contains at runtime. Then if you later … Continue reading

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

Get variable values by name in C#

The process for displaying variable values is somewhat convoluted, but it’s not too long. The example starts by using the following code to define some private and public fields. // Some form-level values. private string private_value1 = “This is private … Continue reading

Posted in arrays, reflection, variables | Tagged , , , , , , , , , , , , , , , | 1 Comment