❔ Lists
i am doing a course and the instructor wanted us to create a list that prints even numbers from 100 to 170.
his solution is below and it confuses me because I don't know why he put "public static List<int> Solution()" just to create another list later.
What's the need of the first list?
public class ListsExercise {
public static List<int> Solution() {
// TODO: write your solution here
//create a new list
List<int> myList = new List<int>();
//go thorugh every number beyweem 100 and 170
for (int i = 100; i <= 170; i++) {
//check if its an even number
if (i % 2 == 0) {
//add it to the list
myList.Add(i);
}
}
//return the list
return myList;
}
}
4 Replies
public static List<int> Solution()
isn't creating a list, it's defining a function called Solution
that returns a list (the return myList;
part)
The stuff in the {}
is the body of the functionWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.thanks so much @vdvman1 that makes so much more sense!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.