c# how to get the length of a list code example
Example 1: get list length c#
public List<int> values;
public int listLength;
listLength = values.Count;
Example 2: c# list length
List<int> list = new List<int>() {7,5,1,4 };
Example 3: 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);
// Output:
// Number of 2s in list is: 2