count in c# code example

Example 1: c sharp list length

// To get the length of a List use 'List<T>.Count'
List<string> stringList = new List<string>{"string1", "string2"};
stringList.Count
// Output:
// 2

Example 2: c# count element in an array

string[] empty = new string[5];
var totalElements = empty.Count(); //5

string[] animals = { "Cat", "Alligator", "fox", "donkey", "Cat", "alligator" };
totalElements = animals.Count(); //6

int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2, 5, 6, 2, 2 };
totalElements = nums.Count(); //12

Example 3: get list length c#

public List<int> values;
public int listLength;

listLength = values.Count;