C# For Beginner Tutorial – Arrays

In the 4th tutorial, we will learn how to use arrays and loops with arrays. We finish the beginner level, then we will produce content for the intermediate level.

Our main topic in this tutorial, What are Arrays? Array Syntax, Array Length, and Array Sort, System.Linq Library, Foreach Loop And Arrays.

After this tutorial, we will learn the methods in the final tutorial at the beginner level. It is recommended that you repeat all topics before proceeding to the Intermediate level.

What Are Arrays?

Array is used to store the same type of data. You can store as many data of the same type as you want. For example, if you want to store numbers up to 10, you can do it with arrays.

Arrays assign all values of the specified data type to a variable, and we can access the data in the variable by the indexing method.

In the Syntax section, we will see all the concepts here in the code, here we only need to understand the general theory.

Array Syntax

We can use 2 different methods to create an array, one is to create the array and then assign the values and the other method is to assign the values while creating.

int[] numbers = {1 , 2 , 3};

int[] numbers2 = new int[3];
numbers2[0] = 1;

The first method is suitable for predetermined arrays, but you should use the second method for subsequent events.

Now let’s see how to create arrays of data types other than int. The same methods are used, we will use the first method in this section, but you should learn both.

string[] names = {"David" , "Mark"};
int[] ages = {10 , 20};
float[] weights = {1.80f , 1.75f};
bool[] isAlive = {true , true};

Now let’s see how to get data. We use indexing to access the data in an array. the first value is always at index 0

string[] names = {"David" , "Mark" , "Alice"};

// Write First And Last Value
Console.WriteLine(names[0] + " " + names[2]);
Output: David Alice

Array Length And Array Sort

We will learn 2 properties about arrays, length is the length of the so array the number of values in it. Sort is used to sort the data from the lowest to the highest.

string[] lt = {"a" , "ab" , "abc"};
int[] nums = {21 , 10 , 11};

Array.Sort(nums);
Array.Sort(lt);

Console.WriteLine(lt[0] + " " + lt[1]);
Console.WriteLine(nums[0] + " " + nums[1]);
Output: 
a ab
10 11

As you can see, we can sort both strings and numbers in descending order, thanks to the Sort function. Let’s look at the Length method now.

string[] lt = {"a" , "ab"};
Console.WriteLine(lt.Length);
Output: 2

As you can see, it gives us the sum of the data, if I add a new data, its length will be 4. Although it does not seem to work at the moment, it will work in other departments.

System.Linq Library

Thanks to this namespace, you can do more with arrays. This namespace offers sum to get the sum of your data, max to see the largest value, and min to see the array’s smaller value.

using System.Linq;

int[] nums = {1 , 2 , 3};

Console.WriteLine("Max: " + nums.Max());
Console.WriteLine("Min: " + nums.Min());
Console.WriteLine("Sum: " + nums.Sum());
Max: 5
Min: 1
Sum: 15 

New Way To Create Array

We have another method that is widely used for creating arrays, similar to the first method we’ve shown.

int[] numbers = int[3] {1 , 2 , 3};

If you create an array with this method, this array will be adjusted so that it can only take 5 data, as the value in the parenthesis increases, the number of values it can take increases.

Foreach Loop And Arrays

We said that we cannot use arrays with foreach after learning about arrays while processing the last loops. Now we will learn how to use arrays with loops.

In the previous lesson, we saw the foreach loop with strings and the main event was that the foreach loop saved the values according to the index in the other variable, now let’s see the same on the array.

int[] nums = {1 , 2 , 3 , 4};

foreach(var i in nums)
{
   Console.WriteLine(i);
}
1
2
3
4
5

As you can see, the variable received a new index every time the loop returns and writes it to the screen. Let’s try changing the types of data.

string[] lt = {"a" , "b" , "c" , "d"};

foreach(var i in lt)
{
   Console.WriteLine("Index: " + i);
}
Index: a
Index: b
Index: c
Index: d

Leave a Reply

Your email address will not be published. Required fields are marked *