- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the total number of elements in the List in C#?
To count the total number of elements in the List, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args) { List<String> list = new List<String>(); list.Add("One"); list.Add("Two"); list.Add("Three"); list.Add("Four"); list.Add("Five"); Console.WriteLine("Elements in List1..."); foreach (string res in list) { Console.WriteLine(res); } Console.WriteLine("
Count of elements in list = "+list.Count); list.Clear(); Console.WriteLine("
Count of elements in list (updated) = "+list.Count); } }
Output
This will produce the following output −
Elements in List1... One Two Three Four Five Count of elements in list = 5 Count of elements in list (updated) = 0
Let us now see another example −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args) { List<String> list = new List<String>(); list.Add("100"); list.Add("200"); list.Add("300"); list.Add("400"); list.Add("500"); Console.WriteLine("Count of elements in the list = "+list.Count); Console.WriteLine("Enumerator iterates through the list elements..."); List<string>.Enumerator demoEnum = list.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } list.Add("600"); list.Add("700"); Console.WriteLine("Count of elements in the list (updated) = "+list.Count); } }
Output
This will produce the following output −
Count of elements in the list = 5 Enumerator iterates through the list elements... 100 200 300 400 500 Count of elements in the list (updated) = 7
- Related Articles
- How to count the total number of frames in OpenCV using C++?
- Count total bits in a number in C++
- Total number of elements present in an array in C#
- How to count total number of occurrences of an object in a Python list?
- Count the number of masked elements in Numpy
- How to count the number of items in a C# list?
- How to count the total number of lines in the file in PowerShell?
- C# program to count total bits in a number
- Count total number of digits from 1 to N in C++
- Count number of elements between two given elements in array in C++
- Count the number of elements in a HashSet in Java
- Count number of smallest elements in given range in C++
- How to count the total number of links in Selenium with python?
- How to count the total number of frames in Selenium with python?
- How to count the total number of links in a page in Selenium?

Advertisements