Learn C#_ Learn C#_ Arrays and Loops Cheatsheet _ Codecademy
Learn C#_ Learn C#_ Arrays and Loops Cheatsheet _ Codecademy
Cheatsheets / Learn C#
C# Arrays
In C#, an array is a structure representing a fixed length // `numbers` array that stores integers
ordered collection of values or objects with the same
int[] numbers = { 3, 14, 59 };
type.
Arrays make it easier to organize and operate on large
amounts of data. For example, rather than creating 100 // 'characters' array that stores strings
integer variables, you can just create one array that
string[] characters = new string[] {
stores all those integers!
"Huey", "Dewey", "Louie" };
Declaring Arrays
In C#, one way an array can be declared and initialized // `numbers` and `animals` are both
at the same time is by assigning the newly declared
declared and initialized with values.
array to a comma separated list of the values
surrounded by curly braces ( {} ). Note how we can int[] numbers = { 1, 3, -10, 5, 8 };
omit the type signature and new keyword on the right string[] animals = { "shark", "bear",
side of the assignment using this syntax. This is only
"dog", "raccoon" };
possible during the array’s declaration.
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-arrays-and-loops/cheatsheet 1/5
10-02-2024 16:03 Learn C#: Learn C#: Arrays and Loops Cheatsheet | Codecademy
In C#, the elements of an array are labeled // Initialize an array with 6 values.
incrementally, starting at 0 for the first element. For
int[] numbers = { 3, 14, 59, 26, 53, 0 };
example, the 3rd element of an array would be indexed
at 2, and the 6th element of an array would be indexed
at 5. // Assign the last element, the 6th
A specific element can be accessed by using the square
number in the array (currently 0), to 58.
bracket operator, surrounding the index with square
brackets. Once accessed, the element can be used in numbers[5] = 58;
an expression, or modified like a regular variable.
C# Array Length
C# For Loops
A C# for loop executes a set of instructions for a // This loop initializes i to 1, stops
specified number of times, based on three provided
looping once i is greater than 10, and
expressions. The three expressions are separated by
semicolons, and in order they are: increases i by 1 after each loop.
Initialization: This is run exactly once at the for (int i = 1; i <= 10; i++) {
start of the loop, usually used to initialize the
Console.WriteLine(i);
loop’s iterator variable.
Stopping condition: This boolean expression is }
checked before each iteration to see if it should
run.
Console.WriteLine("Ready or not, here I
Iteration statement: This is executed after each
iteration of the loop, usually used to update the come!");
iterator variable.
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-arrays-and-loops/cheatsheet 2/5
10-02-2024 16:03 Learn C#: Learn C#: Arrays and Loops Cheatsheet | Codecademy
A C# foreach loop runs a set of instructions once for string[] states = { "Alabama", "Alaska",
each element in a given collection. For example, if an
"Arizona", "Arkansas", "California",
array has 200 elements, then the foreach loop’s body
will execute 200 times. At the start of each iteration, a "Colorado" };
variable is initialized to the current element being
processed.
foreach (string state in states) {
A for each loop is declared with the foreach keyword.
Next, in parentheses, a variable type and variable name // The `state` variable takes on the
followed by the in keyword and the collection to value of an element in `states` and
iterate over. updates every iteration.
Console.WriteLine(state);
}
// Will print each element of `states` in
the order they appear in the array.
C# While Loop
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-arrays-and-loops/cheatsheet 3/5
10-02-2024 16:03 Learn C#: Learn C#: Arrays and Loops Cheatsheet | Codecademy
C# Do While Loop
C# Infinite Loop
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-arrays-and-loops/cheatsheet 4/5
10-02-2024 16:03 Learn C#: Learn C#: Arrays and Loops Cheatsheet | Codecademy
C# Jump Statements
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-arrays-and-loops/cheatsheet 5/5