Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary<TKey,TValue>.Values - does it allocate new memory and time complexity

I am new to C# and I want to understand what is the memory and time overhead of using Dictionary.Values property. Here I could find nothing about the algorithm time complexity specifications or about memory complexity? Am I looking at wrong place or is it undefined in C# spec?

P.S. I am coming from C++ bg.

like image 274
Narek Aghekyan Avatar asked Dec 17 '22 21:12

Narek Aghekyan


1 Answers

is it undefined in C# spec?

It isn't defined in the C# spec because this isn't a C# feature - it is a framework implementation detail.

.Values is lazily instantiated once the first time it is requested; after that, the existing value is handed out. So yes, some memory might be allocated when it is first used. However, it doesn't contain a snapshot copy of the values, so : this isn't an expensive allocation. It simply contains a reference back to the parent dictionary instance.

private Dictionary<TKey, TValue> dictionary;

is literally the only field in ValueCollection<TKey, TValue>. It is essentially a facade that provides a value-centric view over the same data.

like image 69
Marc Gravell Avatar answered May 19 '23 19:05

Marc Gravell