how to count up list of items on c# code example
Example 1: c sharp list length
List<string> stringList = new List<string>{"string1", "string2"};
stringList.Count
Example 2: c# count element in an array
string[] empty = new string[5];
var totalElements = empty.Count();
string[] animals = { "Cat", "Alligator", "fox", "donkey", "Cat", "alligator" };
totalElements = animals.Count();
int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2, 5, 6, 2, 2 };
totalElements = nums.Count();
Example 3: c# count items in listbox
listBox1.Items.Count
Example 4: get count of specific objects in list c#
var myList = new List<int>()
{
0, 1, 2, 1, 5, 4, 8, 5, 2, 7, 1, 9
};
int count = 0;
foreach (var number in list)
{
if (number == 2)
count++;
}
Console.WriteLine("Number of 2s in list is: " + count);