I have a class with a ReadOnlyCollection property. I need to convert that ReadOnlyCollection into a int[]. How can this be done? Is it possible to do this without iterating over the collection?
I have a class with a ReadOnlyCollection property. I need to convert that ReadOnlyCollection
Share
No, it’s not possible to convert a ReadOnlyCollection to an array without iterating it. That would turn the collection to a writable collection, breaking the contract of being read-only.
There are different ways of iterating the collection that spares you of writing the loop yourself, for example using the CopyTo method
Or the extension method ToArray: