C#

All you really need to know about arrays in C#5 min read

I propose today, a ticket oriented for beginners who presents the different uses of arrays in C# (csharp).

I’m going to talk here about the basics of language that is important to know. I think this article may also interest the more experienced (I learned some tricks during my research).

Basics

Statement

Arrays are data structures used to process a set of information.
Very often, the developers use arrays to do calculations on numbers (calculation of average, minimum, maximum) or also to sort elements (a list of names for example).




Arrays in C# (array) always start at index 0 (zero). The syntax for reading and writing in an array is:

To declare an array in C #, use the syntax “[]” associated with a type.
It will also be necessary to allocate the necessary memory space for the board before using it.

These two operations can be done by writing a line (in the example, I create an array of integers (int)):

We have just seen what is needed to declare and use a table in a simple way. Let’s move on to more advanced uses.

Initialization

There are different ways to initialize an array in C #:

  • either during the declaration (it’s a bit like a builder),
  • either by the code (by executing a loop for example).

Here’s how to initialize a table simply when you have only a few values to set (initialization to declaration):

When you have many values or if you want to calculate the values in a more complex way, you will have to initialize the array with the code. Here is a simple example:

Reading All Elements in C# Array

To read or write the values of an array you can use a loop of type “for”, “while” or “do … while”.
In C#, there is also “foreach” which allows you to browse a list of elements including tables.

This structure is interesting because it simplifies the code. Here is an example :

Note: This type of loop is usable only if you do not need to know the index of the table during its course.

The different types of arrays

In C#, there are three types of arrays:

  • One-dimensional paintings (the ones we have seen so far),
  • Arrays with several dimensions “rectangles”,
  • Multi-dimensional “non-rectangular” arrays (jagged array).

One-dimensional arrays

These paintings are are that are most often used in everyday life. The index corresponds to an integer that allows access to an element (for example: each line has the same number of columns).

Multi-dimensional arrays “rectangles”

This time, to access an item you will use multiple indexes. There will be as many indexes as there are dimensions.

Rectangular multi-dimensional arrays are arrays for which the number of elements is constant for each index.

The simplest to understand is the two-dimensional array (that’s where the rectangle image comes from). Here is an example of a statement:

You can also initialize your tables when declaring:

Multi-dimensional “non-rectangular” arrays (jagged array)

In C #, it is possible to create “non-rectangular” multi-dimensional arrays, which will allow, very often, to optimize the memory.

This type of table is simply a table painting. Here is an example :

In this example, I created an array of 3 lines. The first line will have two values, the second line will have three values, and the last one will contain four values.

You can of course initialize the different lines as a “classic” painting:

Rest assured, this type of table is rarely used. It will nevertheless be necessary for some algorithms that deal with many values and for which each “line” or “column” does not necessarily have the same number of elements.

The operations

Here’s how to use your tables to search or do calculations.

The Array class

The class Array is the base class of all tables in C #. I invite you to consult the Microsoft documentation for reference.

Here are some interesting elements of the Array class:

  • Length property: it returns the number of elements in the array,
  • Rank property: it indicates the number of dimensions in the table (works only if it is of the “rectangular” type),
  • Clear() method resets the values ​​to 0 or null (depending on the data type),
  • Copy() methods allow you to copy elements from one array to another,
  • Find() and FindAll methods allow you to find items in a table,
  • Sort() methods are used to sort the values.

Caution: The static methods of the class Array must be used by using the name “Array”. You will understand better by reading the example below.

 

 

 

 

Leave a Comment