I wonder if there is a better way to initialize an array of an object of a reference type, for example.
Queue<int>[] queues = new Queue<int>[10]; for (int i = 0; i < queues.Length; i++) queues[i] = new Queue<int>();
I tried Enumerable.Repeat, but all elements of the array belong to the same instance,
Queue<int>[] queues = Enumerable.Repeat(new Queue<int>(), 10).ToArray();
I also tried Array.ForEach, but it does not work without the ref keyword:
Queue<int>[] queues = Array.ForEach(queues, queue => queue = new Queue<int>());
any other idea?
source share