+ 2
What is the use of the toArray() method in C# Dictionary collection
I was solving the final project in the c# COURSE (Coffee time) and noticed we had to use the line coffee.keys.ToArray() to iterate through the dictionary. Why not just use coffee.keys as in the examples in the lessons. So does the ToArray method specifically do in this case. Thanks in advance
2 Answers
+ 2
Thanks Jayakrishnađźđł
+ 1
coffee.keys returns a list of keys index based. You can use it but modifying the map will result in undefined behaviour. So it won't allow modifying map.
But using ToArray() returns a static array so it is a original list of keys copy. Allows you to work on original map.
If you want modify map in loop use ToArray() method.