c sharp

C# Array length

An array is said to be a variable that can hold multiple values in it or a variable that is a multi-storage mutable that can hold values without creating new variables. Arrays can be defined statically by specifying their length at the time of initialization and can be defined dynamically by not specifying any length. There may come a situation when you must deal with large arrays, and you may find yourself unable to get the length of an array. If faced with that situation, C# length functions can be an essential to let you know about the exact length of arrays. So, this tutorial will be all about those functions to get the lengths of an array. Let’s make our article start with the update of our Linux system i.e. Ubuntu 20.04. For this, start your shell application. We have been utilizing the apt package in the “update” instruction for this purpose. The system will get up to date in no more than 15 seconds.

Now, it’s time to create a C# file in which we have to create some .Net code. This file must be saved in the home directory. Therefore, we have been utilizing the current home located in the terminal and creating it with the “touch” instruction. We have named it “length.cs”. On listing the home folder contents with the list command, we have got the newly created file as well.

Example 01:

In the first example, we will look at getting the length of a string type array. You need to open this newly created file in any of the Linux editors. For convenience, we used the simple text editor (i.e. insert, update, save and exit.) We have been starting our first example with the C# main “System” library which is a must in each C# code to make it work properly.

We have been using the keyword “using” to use the System library. After this, we have been starting a user-defined “Test” class with the keyword “class” followed by the brackets. Everything will be performed within this class. Every program in C# is executed using the main() function of C#. So, we have started the static void main() function by initializing a string array named “Arr” and assigning it some string values. Here comes the “for each” loop to iterate the values of a string array “Arr”. Each value “val” in array “Arr” will be printed out on the shell followed by a space using the “Write” function of C#’s Console class. The Console class is the most used generic class of C# that is purposely designed to hold read, and write functions.

The very next Write() function of the Console class has been used to give a line break and display “Length of Arr” on the shell. The next Write() function for the Console class is used to get and display the length of an array “Arr” calling the “Length” function of the Console class with the “dot” product. The last Write() function has been adding a line break again. We have closed the main() function and Test class at the end. Saved the code with Ctrl+S.

Now the code is ready to be used on the shell. We have used the “mcs” compiler for “C#” in the Ubuntu shell to compile the file “length.cs” in an instant. The length.exe file got created and we have used the mono-runtime to execute this executable file on the shell. The array “Arr” got displayed on the first line and the total length of this array has been displayed on the next line i.e. 5.

Example 02:

We have covered the first example with the use of the Length() function from the Console class to get the length of a string type array. It’s time to get the length for some integer-type arrays in C#. We have started this example with the same System library and user-defined “Test” class. Within the static void main() function, we have initialized 2 arrays of integer type. The first array A1 is 2-dimensional while the array A2 is 3-dimensional.

According to our information, the Length function must work on both the array same as it works on any simple and 1-dimensional array without any problem. The result is, the first two Write() functions of the Console class have been used to display the length of the 2-dimensional array “A1” using the “Length” function by calling it within. The 3rd and 4th Write() function for the Console class is used to display the length of 3-dimensional array A2 on the shell with the help of a “Length” function for C#. The last Write function has been used to give a line break using the “\n” character with the Console class. The main() program and the class Test have been completed and closed here with brackets. Save this code and exit the file to run it on the shell.

We have been using the “mcs” compiler for C# again on the shell to compile our length.cs file. It created a length.exe executable file in the home folder. We have used the mono-runtime command to execute our executable file for C# and got the below-shown output. It displayed the length of A1 as 10 and length of A2 as 12 i.e. length is equal to the total items in an array.

Example 03:

Both the above examples were using the Console class “Length()” function to get the array length for some different dimensional integer arrays and string arrays. Now, we will be looking at another function i.e. “Count” function to get the length of arrays. To use the “Count” function, we have been using the “Linq” namespace of the System library that holds the Enumerable class. This Enumerable class contains the “Count” function in it.

Use of the “System” library is a must. The Test class and main() function has been started. An empty array A1 of size 10 has been defined and Count() function is using it to get the length. The result will be saved to variable v1 and the WriteLine() function of the Console class will be used to display it on the shell. Then we have initialized another array A2 with some integer values and used the Count() function to find its length or the total number of elements. The result will be saved to variable v2 and the WriteLine() function of the Console class will display it on the shell screen.

On compiling the length.cs file and running the length.exe file on the shell, we have got the length of both arrays i.e. 10 and 12 respectively.

Conclusion:

This article’s introduction explains the definition and use of arrays in different programming languages and the types of arrays we can make i.e. dynamic vs static. The first 2 examples elegantly demonstrates the use of the Length() function for the Console class to find out the length for string arrays, 2-dimensional, and 3-dimensional integer arrays. The last example is utilized to demonstrate the use of the Count() function for the Linq namespace of C# to do the same task. Both the functions do the same work and one can use them alternatively.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.