Does anyone happen to know if, in .NET (4.0, if it matters), the Length property of an array, or the Count property of a List<T> is stored after it’s been calculated, until the array/list is changed?
I ask because a record linkage program I’m working on is already horrifically complex enough as it is, and I’d rather not add another O(n) on top of things if I can help it.
Along similar lines, is the hashcode of an instantiated System.String memoized? Looking at it through the debugger, I can see that List<T> has a private _size member that could be where Count gets its value, but I see nothing for int[] or string indicating that they store it anywhere.
I can see the size vs. speed tradeoff, but can anyone tell me for sure if there are backing fields that hold these? For example, since in C#, strings are immutable, wouldn’t it make sense to calculate the hashcode the first time GetHashCode is called, and just store it for every later use?
Using reflector the implementation of
List<T>.Countis:I’m not sure about
Array.Length. The code for it is: