Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Get the number of elements contained in SortedList
Next article icon

C# | Count the total number of elements in the List

Last Updated : 01 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.count?view=netframework-4.7.2


Next Article
C# | Get the number of elements contained in SortedList

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-Collections-Namespace
  • CSharp-Generic-List
  • CSharp-Generic-Namespace

Similar Reads

  • C# | Get the number of elements contained in the Stack
    Stack represents a last-in, first out collection of object. Stack<T>.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<T> 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<TSource>(): 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: // C# Program
    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<T>.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<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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences