C# | Count the total number of elements in the List (original) (raw)
Last Updated : 01 Feb, 2019
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:
CSharp `
// 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:
CSharp `
// 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:
Similar Reads
- C# | Get the number of elements contained in the Stack Stack represents a last-in, first out collection of object. Stack.Count Property is used to gets the number of elements contained in the Stack. Retrieving the value of this property is an O(1) operation. Syntax: myStack.Count Here myStack is the name of the Stack Return Value: The 2 min read
- C# | Get the number of elements contained in the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Count Property is used to get the number of elements contained i 2 min read
- Count the number of element present in the sequence in LINQ? In LINQ, you can count the total number of elements present in the given sequence by using the Count Method. This method returns the total number of elements present in the given sequence. This method can be overloaded in two different ways: Count(): This method returns the total numb 3 min read
- C# | Get the number of elements contained in SortedList SortedList.Count Property is used to get the number of elements contained in a SortedList object. Syntax: public virtual int Count { get; } Property Value: The number of elements contained in the SortedList object. Below programs illustrate the use of above discussed method: Example 1: CSharp // C# 2 min read
- C# | Total number of elements present in an array Array.GetLength(Int32) Method is used to find the total number of elements present in the specified dimension of the Array. Syntax: public int GetLength (int dimension); Here, dimension is a zero-based dimension of the Array whose length needs to be determined.Return value: The return type of this m 2 min read
- C# | Get the number of elements in the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Count Property is used to get the number of elements in the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elemen 2 min read
- C# | Get the number of elements contained in Collection Collection.Count property is used to get the number of elements actually contained in the Collection. Syntax: public int Count { get; } Return Value: The number of elements actually contained in the Collection. Below given are some examples to understand the implementation 2 min read
- C# | Count the number of key/value pairs in the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Count Property is used to get the total number of the key/value pairs contained in the Hashtable. Syntax: myTable. 2 min read
- C# | Number of elements contained in the BitArray The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Count property is used to get the number of element 2 min read
- C# | Get the number of elements actually contained in the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Count property gets the number of elements actually contained in 3 min read