Tag Archives: arrays

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

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