0

I have a program which fill and print list

var numbers = new List<int>();
            

          var numbers = new List<int>();

            int size = new Random(500).Next(10, 15);
            for (int i = 0; i < size; i++)
            {
                numbers.Add(new Random().Next(1,10000));
            }

            numbers.ForEach(
                i => {
                    Console.WriteLine("Element : "+ i);
                }
            );

result :

Element : 6291
Element : 598
Element : 5247
Element : 4152
Element : 8666
Element : 1334
Element : 3225
Element : 3330
Element : 858
Element : 253
Element : 8359
Element : 4735
Element : 342
Element : 6953

Question : Can i write a list printing with enumerable variable but not using a classic loops , i need something like Java Stream Api , something with range ? :

Element by index 0 : 6291
Element by index 1 : 598
Element by index 2 : 5247
.....


andriy-byte
  • 21
  • 1
  • 4
  • 1
    Also note that you should make one `Random()` and re-use it, not make a new one for each iteration of the loop. – Martin Costello Jun 04 '21 at 08:48
  • 3
    It would also help if you could explain *why* you don't want to use the obvious `for` loop. If you know you've got a `List`, and you want to use the index, I'd expect a normal `for` loop to be the simplest option. – Jon Skeet Jun 04 '21 at 08:48
  • https://stackoverflow.com/questions/67829679/how-to-display-only-a-certain-range-of-ids-with-lambda-expression/67829853#67829853 –  Jun 04 '21 at 08:58

0 Answers0