Gets the number of elements contained in the List in C#

C# Count absolute number of rundown components: Here, we will figure out how to tally a complete number of components of a rundown utilizing List.Count property?

Given a rundown, and we need to tally its all outnumber of components utilizing List.Count property.

C# List

A rundown is utilized to speak to the rundown of the items, it is spoken to as List, where T is the sort of the rundown objects/components.

A rundown is a class which goes under System.Collections.Generic bundle, so we need to incorporate it first.

List.Count property

Check is a property of List class; it restores the all out number of components of a List.

Syntax:

    List_name.Count;

Here, List_name is the name of info/source list whose components to be checked.

Example:

    Input:
    //an integer list
    List<int> int_list = new List<int> { 10, 20, 30, 40, 50, 60, 70 };

    //a string list 
    List<string> str_list = new List<string>{
	    "Manju", "Amit", "Abhi", "Radib", "Prem"
    };

    Function call:
    int_list.Count;
    str_list.Count;

    Output:
    7
    5

C# program to tally the all out number of components of a List:

using System;
using System.Text;
using System.Collections.Generic;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //an integer list
            List<int> int_list = new List<int> { 10, 20, 30, 40, 50, 60, 70 };

            //a string list 
            List<string> str_list = new List<string>{
				"Manju", "Amit", "Abhi", "Radib", "Prem"
			};
    
            //printing total number of elements
            Console.WriteLine("Total elements in int_list is: " + int_list.Count);
            Console.WriteLine("Total elements in str_list is: " + str_list.Count);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

Total elements in int_list is: 7
Total elements in str_list is: 5

Leave a Comment

error: Alert: Content is protected!!