Home > Articles > Programming > C#

Magic of Arrays Using C#

  • Print
  • + Share This
If you store data for many customers in memory and then send it in batches to a database, how can you optimize the process? Learn the importance of arrays and how to use specific types of arrays to solve particular problems.
Like this article? We recommend

Like this article? We recommend

Arrays are always a part of any high-level programming language. We can think of arrays as the first step to complex data structures or call them basic data structure. When we start to talk about the user-defined data types (abstract data types) in any language, we first talk about arrays, although the user does not really have the freedom to alter the basic structure of the data type. Still, there are some handy things we can do with arrays.

Well! If you are into programming, you are familiar with the concept of arrays by programming in C, C++, Java, or any other language. Moreover, we usually feel that there is nothing special about arrays; most of us ignore the power of arrays, and use them as normal primitive data types. This article discusses when and why to use a certain type of array to solve a certain problem.

In most languages, what we know about arrays include the following:

  • Linear type data structure

  • Contiguous memory collection of a particular data type (primitive or abstract)

  • Control over elements through subscripts

  • Early time (static) and late (dynamic) time binding

C# gives a bit more to work with, however. This article discusses two types of arrays in depth, in the context of C#:

  • Multi-dimensional arrays

  • Jagged arrays

Before jumping directly to the multi-dimensional and jagged arrays, we'll take a quick look at the basic concepts of C# arrays. If you are already familiar with the syntax of C# arrays and its benefits, you can skip the next section ("What's Special about Syntax"), but if you want to know how syntax can improve things, read the following section.

What's Special about Syntax

Arrays Declaration

We can declare an array in different languages, including C#.

int arr[5] ;  // Declaring an array in C or C++
int[] arr ;  // Declaring an array in C#

C# syntax is very similar to C and C++, except that the [] symbol is placed before the identifier.

Syntax provided by C# will be a bit hard to adapt to by programmers coming from C, C++, or other similar languages because there is no backward-compatibility with other vendor languages' syntax, including Microsoft. But, of course, there must be some strong reason...

What if we want to declare four arrays?

int arr1[5], arr2[5], arr3[5], arr4[5] ; // Declaring arrays in C or C++
int[] arr1, arr2, arr3, arr4 ;  // Declaring arrays in C#

Yes! We don't need to repeatedly insert the '[]' symbol in front of every identifier to prove it's an array because in C# we assume that 'int[]' is an integer-array data-type that can be used as any other normal primitive data type. But when I explained this to one of my friends, he said, "Okay! We agree that this is a positive point, but with the syntax of C and C++-like languages, we can also insert a simple integer variable declaration in-between the arrays declaration." For the sake of simplicity, the following example answers my friend:

// Declaring arrays and integer variable 'var' in C or C++ 
int arr1[5], arr2[5[], var, arr3[5], arr4[5] ;

// Wrong declaration in C# for our purpose
int[] arr1, arr2, var, arr3, arr4 ; 
// Declaring arrays and integer variable 'var' in C#

As you can see, this declares an integer type variable 'var' in C and C++ in a normal way in the same line, whereas in C#, I have written it as a wrong declaration.

What's wrong with C# declaration for integer variable 'var'?

We can see in the beginning of declarations in C and C++-like languages that the keyword is 'int'; whereas in C# declarations it is started with 'int[]', not 'int'—and that makes a great difference. In the syntax of C#, in which 'int[]' can be considered as an integer-array data type, every variable is an integer array; in the case of C and C++-like languages, every variable is an integer until we specify the '[]' symbol in front of the identifier. This is why 'var' is not a simple integer in a C# declaration; instead, it is also an array like other arrays.

So, what if we really we want to declare an integer variable and four arrays in C#? The solution is the following:

// C# declarations
int[] arr1, arr2, arr3, arr4 ;
int var ;

This is the way we can declare four arrays in a different block with an 'int[]' data type and an integer-only declaration with an 'int' data type. So, in all cases (C, C++, and C#), we have declared four arrays (arr1, arr2, arr3, arr4) and one integer variable ('var'). In C#, however, we have to declare our integer variable 'var' in a different block (line here), but we get a loss of flexibility and waste of time in that case.

Is this really a waste of time, or is something hidden? Yes! This gives us a better programming approach, which is very important to the programmer and to his company, too.

The readability of programs is the success key of any IT project. In the absence of certain rules imposed on the programmers to maintain the readability of the program, companies as well as programmers suffer when they go back to their source code after a long time. Also, if a programmer leaves a company, then the replacement programmer has to fight a lot to understand the source code written by the old programmer. In this case, companies have to invest lots of energy to get things back on the right track, and sometimes they have to scrap most of the work done by the old programmer (again, lots of time and money wasted).

For example, suppose we want to search all the integer-type variables defined in the program (not arrays). In C and C++-like language syntax, we have to go through every line that starts with the 'int' keyword; then we have to identify the integer variables and ignore the array declarations. But in the case of C# syntax, this is crystal-clear. If there is an 'int[]' at the beginning of the line, then all variables are integer arrays; in the case of the 'int' keyword, all the variables are integer variables only. This really saves a lot of time, not only for a newly employed programmer who is fighting to understand the code written by his predecessor. It can also help the programmer when he returns to his code after a long time or when he is debugging his code.

After this discussion of array syntax, we can easily assume that we have the same benefits with two- dimensional or multi-dimensional arrays. The syntax is the following:

int[ , ] arr ; //Two Dimensional array declaration
int[ , , ] arr ; //Three (Multi) Dimensional array declaration

And accessibility is the same, as the syntax shows. For example, to access the second element of the second row (if this exists), the syntax is arr[1,1] ;.

The other member of the family of the array series is the jagged array. Jagged arrays are simply arrays of arrays (jagged arrays contain other arrays inside).

int[][] arr ; //Declaring jagged array : array of arrays

Although this syntax may look familiar to C and C++ programmers for two-dimensional (multi-dimensional) arrays, it is actually quite different from multi-dimensional arrays in C#.

The following is a summary of some array declarations:

int[] a1;     // single-dimensional array of int
int[,] a2;     // 2-dimensional array of int
int[,,] a3;     // 3-dimensional array of int
int[][] j2;     // "jagged" array: array of (array of int)
int[][][] j3;   // array of (array of (array of int))

Memory Allocation

In the case of memory allocation, to really store the data into an array, we use the keyword 'new', as we do to instantiate an object. (When using the 'new' keyword, we are allocating memory at run time, not at compile time; C# uses static bindings only when an array has been initialized.).

// In case of single dimentional array to store 5 integer type elements
int[] arr = new int[5] ; 

// In case of two dimensional array to allocate memory 
// for 3 rows and 2 columns
int[ , ] arr = new int [3,2] ; 

For a multi-dimensional array, the syntax is the same as for a two-dimensional array, while the number of indices inside '[]' depends on the dimensions of the array.

For example, for a three-dimensional array:

//Allocating memory for three dimensional array
int[ , , ] arr = new [3,2,2] ; 

Memory allocation is different for jagged arrays:

//In case of jagged array to place 3 rows where we can allocate the memory 
//For different number of columns in every row as shown in following lines
int[][] arr = new int[3][] ;
arr[0] = new int[2] ; // 2 columns in first row
arr[1] = new int[1] ; // 1 column in second row
arr[2] = new int[3] ; // 3 columns in third row

The following is a summary of array initializations:

int[] a1 = new int[] {1, 2, 3};
int[,] a2 = new int[,] {{1, 2, 3}, {4, 5, 6}};
int[,,] a3 = new int[10, 20, 30];
int[][] j2 = new int[3][];
j2[0] = new int[] {1, 2, 3};
j2[1] = new int[] {1, 2, 3, 4, 5, 6};
j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
  • + Share This
  • 🔖 Save To Your Account