Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Practicing Multi-Dimensional Arrays | Arrays
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

Practicing Multi-Dimensional Arrays

It is sometimes useful to declare constants for indexing multi-dimensional arrays. Following is the array from the last chapter's quiz:

cs

main

copy
1234567
const int FRUITS = 0; const int VEGETABLES = 1; string[,] foods = { { "Apple", "Apricot", "Banana", "Grapes", "Lime" }, // fruits { "Tomato", "Cabbage", "Carrot", "Pumpkin", "Broccoli" }, // vegetables };

We have created two constants FRUITS and VEGETABLES with values 0 and 1, representing the rows 0 for fruits, and 1 for vegetables in the foods array.

So if we need to access the "Cabbage" element from vegetables, we can simply write:

cs

main

copy
1
foods[VEGETABLES][1];

This trick might not be too helpful in smaller arrays like this one itself, however when dealing with more complex arrays, constants like these can help in easily accessing items from the array so that we don't need to memorize which row contains which items - in case we have categorized items in rows.

Now the challenge is to create a 2D array of type string and name productOptions, having a 4x3 shape (4 rows, 3 columns). Initialize it with the following data:

Row 1: "Red", "Green", "White" Row 2: "Square", "Rectangle", "Sphere" Row 3: "Thick", "Medium", "Thin" Row 4: "Printed", "Crafted", "None"

Access the "Crafted" element via indexing and output it. Use the relevant constant variable for that.

cs

main

copy
123456789101112131415161718192021222324
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int COLOR = 0; const int SHAPE = 1; const int WIDTH = 2; const int DESIGN = 3; // Create the array below _______ = { _______ }; // Reference the relevant element in the WriteLine statement Console.WriteLine(productOptions[DESIGN, 1]); Console.WriteLine(productOptions[COLOR, 2]); Console.WriteLine(productOptions[SHAPE, 0]); Console.WriteLine(productOptions[WIDTH, 1]); } } }
  1. For declaring a multidimensional array we add commas inside the square brackets ([]) depending upon the dimensions of the array. For-example, a 3D integer array would be declared like int[,,] arrayName = { ... };.
  2. It is valid to use variable or constant values in indexing. For-example if we have an array int[] exampleArray = { 1, 2, 3, 4, 5 }; and a constant const int someIndex = 2, it is valid to write exampleArray[someIndex] for accessing the element 3 from the Array.
cs

main

copy
123456789101112131415161718192021222324252627
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int COLOR = 0; const int SHAPE = 1; const int WIDTH = 2; const int DESIGN = 3; // Create the array below string[,] productOptions = { { "Red", "Green", "White" }, { "Square", "Rectangle", "Sphere" }, { "Thick", "Medium", "Thin" }, { "Printed", "Crafted", "None" } }; // Reference the relevant element in the WriteLine statement Console.WriteLine(productOptions[DESIGN, 1]); Console.WriteLine(productOptions[COLOR, 2]); Console.WriteLine(productOptions[SHAPE, 0]); Console.WriteLine(productOptions[WIDTH, 1]); } } }

Everything was clear?

Section 5. Chapter 6
We're sorry to hear that something went wrong. What happened?
some-alt