C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: This is identical to the List collection, which we will see next. This property access is fast.
ArrayListC# program that uses ArrayList
using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList items = new ArrayList();
items.Add("One");
items.Add("Two");
// Count the elements in the ArrayList.
int result = items.Count;
Console.WriteLine(result);
}
}
Output
2
Part A: List has a property called Count that is fast to access. This property returns 2.
PropertyPart B: This code shows Count() with parentheses. This is a LINQ extension method—it can be confusing if you don't know the difference.
CountIEnumerableC# program that uses List
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
// Part A: count with the Count property.
int result = list.Count;
Console.WriteLine(result);
// Part B: count with the extension method.
result = list.Count();
Console.WriteLine(result);
}
}
Output
2
2
Last element: We use the Length property on arrays, and also the Count property on ArrayList and List, to access the final element.
Tip: The final element is located at the count minus one, except when the length of the array is 0.
C# program that uses array Length
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30 };
// Count with Length property.
Console.WriteLine(numbers.Length);
}
}
Output
3
Tip: Enumerations in C# are collections that haven't been determined exactly yet, and they can use yield and foreach keywords.
C# program that uses LINQ
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] arr = new string[]
{
"One",
"Two",
"Three"
};
// An enumerable collection, not in memory yet.
var e = from s in arr
select s;
int c = e.Count(); // Extension method.
Console.WriteLine(c);
// ... does a lot of work to get the count.
}
}
Output
3
Note: The Count() method was iterating through the collection each time. This has poor algorithmic performance.
Version 1: This version uses the Count() extension method on the 100-element integer array.
Version 2: This version of the code tests the Length property on the array in the tight loop.
Result: Version 2 (which access the Length property) performs much faster and is the preferable code.
BenchmarkC# program that benchmarks Count
using System;
using System.Diagnostics;
using System.Linq;
class Program
{
const int _max = 1000000;
static void Main()
{
var array = new int[100];
// Version 1: access Count() on array.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (array.Count() != 100)
{
return;
}
}
s1.Stop();
// Version 2: access Length on array.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (array.Length != 100)
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
32.49 ns Count()
0.79 ns Length
Note: Occasionally the JIT compiler can optimize loops that include the Count or Length in the for statement.
Note 2: Hoisting is sometimes faster. For optimal performance, you will need to test your program. Not hoisting can be faster.
C# program that shows Count hoisting
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var values = new List<int>() { 10, 20 };
// Loop over values, no hoisting.
for (int i = 0; i < values.Count; i++)
{
Console.WriteLine($"VALUE: {values[i]}");
}
// Loop over values, with hoisting of max.
int max = values.Count;
for (int i = 0; i < max; i++)
{
Console.WriteLine($"VALUE: {values[i]}");
}
}
}
Output
VALUE: 10
VALUE: 20
VALUE: 10
VALUE: 20