Lesson 12: Learning to work with Arrays Arrays in C#

This is our lesson no 12 and today we will get to know about Learning to work with Arrays Arrays in C#.

Learning to Work with Arrays in C#

Arrays are a collection of items, such as string text. You can use arrays to put multiple identical variables in a group and then perform certain actions on them, such as sorting.
use.

Arrays in C # are defined almost like variables, except that an [] is placed in front of the array data type.

The general structure of defining an array in C # is as follows:

string [] names;
To use an array, you need to define and initialize it.

As follows :

string [] names = new string [2];

In the above code, the number 2 specifies the size of the array. The size of the array determines the number of members you can put in the array.

Putting items in an Arrays is easy.

As follows :

names[0] = “John Doe”;

But why did we use the number 1 to define the first member of the array? Because in C #, like other programming languages, counting starts from 1 instead of 1.

So the first member of the array is indexed with 1, the second with 1, and so on.

You need to be careful about the number of members of an array, because defining more members than the number specified for the array will cause the program to fail. Note that when an array is defined with 2 members, its members have indices 1 and 1 and there is no member with 2 indices.

This is a common mistake when using arrays

In previous lessons, you became familiar with repetitive (loop) structures in C #, these structures are very suitable for working with arrays.

The most common way to extract information from an Arrays array is to use loops. Each time the loop is repeated, an array member is extracted and the operation can be performed on it.

In the following practical example, we show how to read and work with an array using a loop:

using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] names = new string[2];
names[0] = “John Doe”;
names[1] = “Jane Doe”;
foreach(string s in names)
Console.WriteLine(s);
Console.ReadLine();
}
31
}
}

In the example above, we used the foreach loop, which is the simplest type of loop to work with arrays.

But you can also use other types of loops in C #, such as the for loop, which can easily count the members of an array and print messages to the output to the number of members.

for(int i = 0; i < names.Length; i++) Console.WriteLine(“Item number ” + i + “: ” + names[i]);

The function of the above code is very simple. We used Length Arrays to determine how many times the loop should be repeated.

Also from the counter (i (Counter) to understand that every moment in
Where is the array processed and used to extract each of its members?

Just as we used a number called an indexer when defining an array, the same number can be used to read and extract array members.

In the previous section, we said that the members of an array can be sorted. This is very convenient.

The Class Array consists of several different methods that you can use to work with arrays.

In the example below, we use numbers instead of string or text to indicate a specific purpose, otherwise we can simply define the members of an array of type string.

There is another much simpler way to fill in and arrange the members of an array, especially when your array members are specified and organized.

are.

As follows :

int[] numbers = new int[5] { 4, 3, 8, 0, 5 };

Using just one line of code, we created an array with 5 members and set five integers as its members.

By filling the members of an array in the above way, you will have another advantage in your code.

With the above method, the compiler checks the number of members defined for the array with the number of your items, and if you provide more than the number of members, the error occurs.

In fact, the above code can be written more concisely as follows, but in this case we lose the automatic check of the compiler:

int[] numbers = { 4, 3, 8, 0, 5 };

But let’s look at how to sort an array. Read the following example carefully:

using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 4, 3, 8, 0, 5 };
Array.Sort(numbers);
foreach(int i in numbers)
33
Console.WriteLine(i);
Console.ReadLine();
}
}
}

The only new thing in the example code above is the Sort.Array command. This method can receive various parameters as input and will arrange an array of types in each.

Example of descending, ascending or ….

The Class Array has various other methods for working with arrays, such as the Reverse () method, which can be used to reverse the order of members of an array. For a better understanding, go to the full list of array methods in C #.

The arrays we used in the examples in this lesson are all one-dimensional, but arrays in C # can also be two-dimensional or three-dimensional. These arrays are also called nested arrays or matrices.

Multidimensional arrays in C # are divided into two categories:

Rectangular Arrays or Rectangular Array. Ray Irregular Arrays or Array Jagged.

The difference between the above two types of arrays is that each dimension of the rectangular arrays must be the same size, for example an array
.4 × 4

But each dimension Array Jagged arrays can have different sizes. Discuss multidimensional arrays
It is very extensive and beyond the scope of this tutorial.

%d bloggers like this: