List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. List.Count Property is used to get the total number of elements contained in the List.
Properties:
- It is different from the arrays. A list can be resized dynamically but arrays cannot be.
- List class can accept null as a valid value for reference types and it also allows duplicate elements.
- If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
Syntax:
list_name.Count
Below programs illustrate the use of Count property:
Example 1:
// C# code to get the number of // elements contained in List using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating a List of integers List<int> firstlist = new List<int>(); // adding elements in firstlist for (int i = 4; i < 10; i++) { firstlist.Add(i * 2); } // To get the number of // elements in the List Console.WriteLine(firstlist.Count); } } |
Output:
6
Example 2:
// C# code to get the number of // elements contained in List using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating a List of integers List<int> firstlist = new List<int>(); // To get the number of // elements in the List Console.WriteLine(firstlist.Count); } } |
Output:
0
Reference:
Recommended Posts:
- C# | Total number of elements present in an array
- Total number of elements in a specified dimension of an Array in C#
- Getting the Total Number of Days in a Month Using If-else and Switch Statements in C#
- How to Get a Total Number of Days in a Month using built in Functions in C#?
- How to sort a list in C# | List.Sort() Method Set -1
- How to sort a list in C# | List.Sort() Method Set -2
- C# | Removing all the elements from the List
- C# | Adding the elements of the specified collection to the end of the List
- C# | How to insert the elements of a collection into the List at the specified index
- C# | Removing a range of elements from the List
- C# | How to get all elements of a List that match the conditions specified by the predicate
- C# | How to check whether a List contains the elements that match the specified conditions
- C# | Reverse the order of the elements in the entire List or in the specified range
- C# | Remove all elements of a List that match the conditions defined by the predicate
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | Get the number of elements actually contained in the ArrayList
- C# | Number of elements in HashSet
- C# | Get the number of elements contained in the Stack
- C# | Get the number of elements contained in the Queue
- C# | Get the number of elements in the SortedSet
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

