c sharp

C# List Length

In C#, there is a list class that has a container in which we can add data in the form of index values by using a dynamically allocated size, unlike arrays. The length of the list having items in it can be acquired through a count() function. This article will show the procedure to measure the length of the list.

Implementation

Example # 1

This example deals with the sample creation of a new list. Since lists are dynamically created containers, we always use a “new” keyword with them. To start the program, we need to use two basic system libraries of C#. Like all the collections and lists, related information is included in the collections of the generic library. We will use these two header files in our programs of lists:

Using system;

Using system. Collections.Generic;

After introducing the libraries, we will move toward the main program; a list will be created. This will contain data of integer data type.

List <int> firstlist = new List <int> ();

In this example, we will not add any element to the list, so the number of elements will be zero. The length of the list can be obtained through the built-in count function of C#. This function call is made through the object of the newly created list. We will call this function through the console class and the writeLine() function to display the contents on the terminal.

Console.writeLine(firstlist.Count);

Save the code. To get the program executed, we will use the MCS compiler. This compiler will compile the code and then execute it. Mono will execute the code.

$ MCS file.cs

$ Mono file.exe

On the execution, you will see that the count function shows the length of the list as zero, since any item is not added to the list.

Now, if we want to add elements to the list, we will use a FOR loop to iterate through the list and add items to it at every index of the list. Since the list contains indexes like arrays, we will access the items through the FOR loop. This loop starts from 2 and ends before the 10th number.

Inside the loop, add() function — the C# built-in function for lists — is used. This function is utilized to add items to the list. Every time we use the add() function, the item is added to the next index in the list automatically.

Firstlist.Add( I * 2);

Again, the length of the list is obtained through the count() function.

Example # 2

In the previous example, we added numbers to the integer list using a loop that starts from a particular number and ends at a specified limit. But, like arrays, lists are also declared and initialized with items manually. Once the list is created, we add items to them. In this example, a string data type variable is used to create the list. It means that it will contain strings, not only integers.

After defining both the libraries, we will create a string variable list.

List <string> student_List = new List <string> ();

After the list of the string is declared, we will start adding values to the list manually through add() function. The string we want to add will be written as an argument in the parameters. Each time, we will integrate this add() function with the object of the list since all the items added to that list are accessed through the object.

Student_list.Add("Anna Hallow");

Similarly, all four more strings will be added. In this illustration, we will use an integer type variable to store the length of the list. This variable will be used directly to display the number of items it contains. This length will be calculated through the count function that is accessed through the object of the list.

Int student_count = student_list.Count;

Now, we will print the results through the variable that stores the number of items in the list. The variable contains the result in integers. This value is converted first into a string because we applied the concatenation process.

This process involves adding two strings to one another. The first string is a statement used, and the second string is the result stored in the variable. The conversion is done through the “Tostring()” built-in string function. This function is accessed through the variable having the number of list elements.

Student_count.Tostring();

Upon execution, you will see that both the strings are concatenated by using the “+” operator used to add both strings.

Example # 3

In addition to the count() function, a capacity function is used to measure the total value of numbers a list can hold in a fixed size. If we want to limit the size of the list up to some extent, then it will have a fixed capacity. Inside the main program, a var type variable is declared. Var type variable is a data type that stands for varchar, which contains characters and integers both in the list or in any other container. Both these functions are accessed through the newly created list numbers. The value is accessed through the dollar sign.

Numbers.cout;

Numbers.capacity;

The capacity of the list is always added in the form of chunks, power of 2, which is the default value of the capacity. In this way, the resizing of the list occurs at intervals. The size of the list doesn’t need to vary.

If a variable of previous capacity whose value is assigned to the capacity of the numbers is declared, ee will use a WHILE loop to generate a series of capacities up to 8 times. Inside the loop, an if-statement is used. If the ‘numbers capacity’ value is not equal to the “prevcapacity” value, the loop will continue iterating. The value in the current capacity is assigned to the “prevcapacity”, and the loop is incremented. In the end, the values of capacity are displayed in every iteration up to the 8th time.

Upon execution of the code, you will see that since any item is not added to the list, the size of the list is zero, but the capacity of the fixed list is always there. If the capacity of the fixed list does not show, we will calculate it by using the capacity function.

Conclusion

The length of the list in C# is obtained by using a count function. This function is easy to use and can be accessed by using the object of the list that uses add() function. The addition of items can be either manually done or through the loop. Both cases are explained very well through examples. Contrary to the count function, the capacity function is used to determine the actual size of the list at the time of creation. The use of capacity and the count function are explained in the article with some elementary examples. Read more articles available on the website for more tips and tutorials.

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.