C# | Get the number of elements contained in Collection<T>
Collection<T>.Count property is used to get the number of elements actually contained in the Collection<T>.
Syntax:
public int Count { get; }
Return Value: The number of elements actually contained in the Collection<T>.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to get the number of // elements contained in the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection<string> myColl = new Collection<string>(); // Adding elements in Collection myColl myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("E"); // To print the count of elements in Collection Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } } } |
chevron_right
filter_none
Output:
Count : 5 A B C D E
Example 2:
// C# code to get the number of // elements contained in the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection<int> myColl = new Collection<int>(); // Adding elements in Collection myColl myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // To print the count of elements in Collection Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } } } |
chevron_right
filter_none
Output:
Count : 4 2 3 4 5
Note: Retrieving the value of this property is an O(1) operation.
Reference:
Recommended Posts:
- C# | Get the number of elements contained in the Queue
- C# | Number of elements contained in the BitArray
- C# | Get the number of elements contained in the Stack
- C# | Get the number of elements contained in SortedList
- C# | Get the number of elements actually contained in the ArrayList
- C# | Get the number of nodes contained in LinkedList<T>
- C# | Get the number of key/value pairs contained in ListDictionary
- C# | Get the number of key/values pairs contained in OrderedDictionary
- C# | Check whether an element is contained in the ArrayList
- C# | Get or set the number of elements that the ArrayList can contain
- C# | Get the number of elements in the SortedSet
- C# | Get or set the number of elements in the BitArray
- C# | Number of elements in HashSet
- Total number of elements in a specified dimension of an Array in C#
- C# | Count the total number of elements in the List
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.



