C# Multidimensional Arrays

Multidimensional Arrays

In the previous chapter, you learned about arrays, which is also known as single dimension arrays. These are great, and something you will use a lot while programming in C#. However, if you want to store data as a tabular form, like a table with rows and columns, you need to get familiar with multidimensional arrays.

A multidimensional array is basically an array of arrays.

Arrays can have any number of dimensions. The most common are two-dimensional arrays (2D). Continue reading C# Multidimensional Arrays

C# Sort Arrays

Sort an Array

There are many array methods available, for example Sort(), which sorts an array alphabetically or in an ascending order:

Example

// Sort a string
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Array.Sort(cars);
foreach (string i in cars)
{
  Console.WriteLine(i);
}
// Sort an int
int[] myNumbers = {5, 1, 8, 9};
Array.Sort(myNumbers);
foreach (int i in myNumbers)
{
  Console.WriteLine(i);
}

Continue reading C# Sort Arrays

C# Loop Through Arrays

Loop Through an Array

You can loop through the array elements with the for loop, and use the Length property to specify how many times the loop should run.

The following example outputs all elements in the cars array:

Example

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.Length; i++) 
{
  Console.WriteLine(cars[i]);
}

Continue reading C# Loop Through Arrays

C# Arrays

Create an Array

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets:

string[] cars;

We have now declared a variable that holds an array of strings. Continue reading C# Arrays

C# Foreach Loop

The foreach Loop

There is also a foreach loop, which is used exclusively to loop through elements in an array (or other data sets):

Syntax

foreach (type 
variableName
 in 
arrayName
) 
{
  // code block to be executed
}

The following example outputs all elements in the cars array, using a foreach loop: Continue reading C# Foreach Loop

C# For Loop

C# For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) 
{
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed. Continue reading C# For Loop

C# While Loop

Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.

Continue reading C# While Loop

C# Switch

C# Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax

switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;
}

Continue reading C# Switch

C# Short Hand If…Else

Short Hand If…Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements:

Syntax

variable = (condition) ? expressionTrue : expressionFalse;

Instead of writing: Continue reading C# Short Hand If…Else

C# The else if Statement

The else if Statement

Use the else if statement to specify a new condition if the first condition is False.

Syntax

if (condition1)
{
// block of code to be executed if condition1 is True
} 
else if (condition2) 
{
// block of code to be executed if the condition1 is false and condition2 is True
} 
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}

Example

int time = 22;
if (time < 10) 
{
  Console.WriteLine("Good morning.");
} 
else if (time < 20) 
{
  Console.WriteLine("Good day.");
} 
else 
{
  Console.WriteLine("Good evening.");
}
// Outputs "Good evening."

 

Continue reading C# The else if Statement