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:
1 2 3 4 5 6 7 | // reading the 13th element int value = array[12]; // write in the 11th element mount[10] = 10; |
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)):
1 2 3 | int[] values = new int[100]; |
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):
1 2 3 4 | // declaration of an array initialized with 5 values int [] values = new int [] {1, 2, 3, 4, 5}; |
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:
1 2 3 4 5 6 7 | // declaration and allocation of an array of 10 elements of the type int int [] values = new int [10]; // initialization of values for (int i = 0; i <10; i ++) values [i] = i * i; |
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 :
1 2 3 4 5 6 | foreach (int value in values) { Console.WriteLine ("value = {0}", value); } |
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:
1 2 3 4 5 6 7 8 9 10 | // declaration and allocation of an array of 10 rows and 20 columns int [,] Rectangular array = new int[10, 20]; // write a value Rectangular array[0, 0] = 1; // reading a value int value = Rectangular array [3, 5]; |
You can also initialize your tables when declaring:
1 2 3 | string[,] users = new string[3, 2] { { "John", "Doe" }, { "Mary", "Smith" }, { "Bill", "Gates" } }; |
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 :
1 2 3 4 5 6 7 | int[][] arrayNoRectangular = new int[3][]; arrayNoRectangular[0] = new int[2]; arrayNoRectangular[0] = new int[3]; arrayNoRectangular[0] = new int[4]; |
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:
1 2 3 | arrayNoRectangular [0] = new int [2] {1, 2}; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | int[] values = new int[] { 3, 5, 1, 2, 3, 2, 3, 4, 2, -1, 33 }; // reverse the table Array.Reverse(values); // show the first value Console.WriteLine(values[0]); // prints 33 // sort (default sort order) Array.Sort(values); Console.WriteLine("{0} | {1}", values[0], values[values.Length - 1]); // display -1 | 33 // search every "2" in the table int[] theTwo = Array.FindAll(values, delegate (int v) { return v == 2; }); Console.WriteLine(theTwo.Length); // prints 3 - there are 3 times the number 2 in the table |