How to add to a new list from a for loop within a method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to add to a new list from a for loop within a method?

I have this method below that lists all the numbers divisible by 5 up to 200, based on the integer input in the method. I want to then, instead of listing the numbers on the console, add them to a new list. Is it possible to do this within the for loop of the method? I appreciate any help. public static void divByFive(int a) { for (; a < 200; a++) { if (a % 5 == 0) Console.WriteLine(a); } }

26th Sep 2017, 5:39 PM
Soygeezy
Soygeezy - avatar
3 Answers
+ 4
Here's the code you're looking for! So you create a List variable like so: List<int> myList = newList<int>(); You don't have to insert a number like you would with an array, because it just adds on behind the list. then to add to the list: myList.Add(integer); I hope this helps you! https://code.sololearn.com/c5CLmeeT4w1v/?ref=app
26th Sep 2017, 5:39 PM
Limitless
Limitless - avatar
+ 2
make a list of integer List<int> list = new List<int>(); list.Add(2); list.Add(3); list.Add(5); list.Add(7); } https://www.dotnetperls.com/list
26th Sep 2017, 5:31 PM
sneeze
sneeze - avatar
0
n/a
26th Sep 2017, 5:38 PM
Soygeezy
Soygeezy - avatar