Key Concepts

Review core concepts you need to learn to master this subject

C# Arrays

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

In C#, an array is a structure representing a fixed length ordered collection of values or objects with the same type.

Arrays make it easier to organize and operate on large amounts of data. For example, rather than creating 100 integer variables, you can just create one array that stores all those integers!

Declaring Arrays

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

A C# array variable is declared similarly to a non-array variable, with the addition of square brackets ([]) after the type specifier to denote it as an array.

The new keyword is needed when instantiating a new array to assign to the variable, as well as the array length in the square brackets. The array can also be instantiated with values using curly braces ({}). In this case the array length is not necessary.

Declare and Initialize array

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

In C#, one way an array can be declared and initialized at the same time is by assigning the newly declared array to a comma separated list of the values surrounded by curly braces ({}). Note how we can omit the type signature and new keyword on the right side of the assignment using this syntax. This is only possible during the array’s declaration.

Array Element Access

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

In C#, the elements of an array are labeled incrementally, starting at 0 for the first element. For example, the 3rd element of an array would be indexed at 2, and the 6th element of an array would be indexed at 5.

A specific element can be accessed by using the square bracket operator, surrounding the index with square brackets. Once accessed, the element can be used in an expression, or modified like a regular variable.

C# Array Length

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

The Length property of a C# array can be used to get the number of elements in a particular array.

C# For Loops

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

A C# for loop executes a set of instructions for a specified number of times, based on three provided expressions. The three expressions are separated by semicolons, and in order they are:

  • Initialization: This is run exactly once at the start of the loop, usually used to initialize the loop’s iterator variable.
  • Stopping condition: This boolean expression is checked before each iteration to see if it should run.
  • Iteration statement: This is executed after each iteration of the loop, usually used to update the iterator variable.

C# For Each Loop

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

A C# foreach loop runs a set of instructions once for each element in a given collection. For example, if an array has 200 elements, then the foreach loop’s body will execute 200 times. At the start of each iteration, a variable is initialized to the current element being processed.

A for each loop is declared with the foreach keyword. Next, in parentheses, a variable type and variable name followed by the in keyword and the collection to iterate over.

C# While Loop

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

In C#, a while loop executes a set of instructions continuously while the given boolean expression evaluates to true or one of the instructions inside the loop body, such as the break instruction, terminates the loop.

Note that the loop body might not run at all, since the boolean condition is evaluated before the very first iteration of the while loop.

The syntax to declare a while loop is simply the while keyword followed by a boolean condition in parentheses.

C# Do While Loop

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

In C#, a do while loop runs a set of instructions once and then continues running as long as the given boolean condition is true. Notice how this behavior is nearly identical to a while loop, with the distinction that a do while runs one or more times, and a while loop runs zero or more times.

The syntax to declare a do while is the do keyword, followed by the code block, then the while keyword with the boolean condition in parentheses. Note that a semi-colon is necessary to end a do while loop.

C# Infinite Loop

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

An infinite loop is a loop that never terminates because its stopping condition is always false. An infinite loop can be useful if a program consists of continuously executing one chunk of code. But, an unintentional infinite loop can cause a program to hang and become unresponsive due to being stuck in the loop.

A program running in a shell or terminal stuck in an infinite loop can be ended by terminating the process.

C# Jump Statements

// `numbers` array that stores integers int[] numbers = { 3, 14, 59 }; // 'characters' array that stores strings string[] characters = new string[] { "Huey", "Dewey", "Louie" };

Jump statements are tools used to give the programmer additional control over the program’s control flow. They are very commonly used in the context of loops to exit from the loop or to skip parts of the loop.

Control flow keywords include break, continue, and return. The given code snippets provide examples of their usage.

  1. 1
    You want to write a program that keeps track of the heights of different plants in your garden. You could do this as a series of variables, but those would become difficult to work with really fast…
  2. 2
    In C#, arrays are a collection of values that all share the same data type. You could have an array of type string that contains a list of your favorite songs, or an array of type int that stores a…
  3. 3
    We often want to know how many items an array contains. We can do this with the .Length property. int[] plantHeights = { 3, 4, 6 }; // arrayLength will be 3 int arrayLength = plantHeights.Length…
  4. 4
    Arrays are useful for storing values, but they’re not very useful if they simply stay there — we also need a way to access them. Arrays order items so that they’re in a specific sequence, w…
  5. 5
    Once we create an array, the size of that array is fixed. However, it’s possible to change the values it contains. For example, we can initialize an array that has a length of three without speci…
  6. 6
    In C#, there are several built-in methods we can use with arrays. The full list can be found in the [Microsoft documentation of the Array class, under methods](https://docs.microsoft.com/en-us/dotn…
  7. 7
    In addition to Sort(), IndexOf(), and Find(), there are several other built-in methods for arrays. You can find them (and you probably guessed it) in the [Microsoft documentation](https://docs.micr…
  8. 8
    Congratulations, we covered a lot in this lesson! We learned about: - Data structures and how we can use them to better organize our data - How to build, access, and edit values in arrays - How to…
  1. 1
    Imagine you’re building a video game and in this game, you want to add 15 aliens to the screen. How do we use code to tell a computer this: “Create a variable and call the method 15 times”? We co…
  2. 2
    Loops are used to repeat a set of instructions based on a set of conditions. If this makes you think of conditional statements, you’re not wrong! The while loop looks very similar to an if statem…
  3. 3
    Similar to the while loop, a do…while loop will continue running until a stopping condition is met. One key difference is that no matter what, a do…while loop will always run once. do { …
  4. 4
    What if we want our code to execute a specific number of times? We can use a for loop to do that. for (initialization; stopping condition; iteration statement) { statement; } The for loop te…
  5. 5
    There’s one more way to give looping instructions to a computer. We define a sequence of values and tell the computer to repeat the instructions for each item in the sequence. foreach (type elem…
  6. 6
    You may have noticed that there are lots of similarities between different types of loops, and you’re right! We just showed how we can use a foreach loop to iterate through an array. But we can a…
  7. 7
    There a few keywords we can use to add further control flow to our loops. Typically, they work with a series of nested loops, where one loop is written entirely within the body of another loop. T…
  8. 8
    Well done! In C#, loops are commonly used because they save time, reduce errors, and are easy to read. Being comfortable with each type of loop will make you a better programmer. In review: - A _lo…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo