Could you use some lambdas:
//load a list, t, with 100 integers List<int> t = Enumerable.Range(1, 100).ToList(); //find odd numbers var oddNumbers = t.Where(num => num%2 != 0); //find even numbers var evenNumbers = t.Where(num => num%2 == 0); //print odd numbers foreach (int i in oddNumbers) Console.WriteLine(i); //print even numbers foreach(int i in evenNumbers) Console.WriteLine(i);
The enumerable just loads the list from 1-100, and then I just grab all the odds / evens and then print them. All this can be reduced to:
var e = Enumerable.Range(1, 100).Where(num => num%2==0);
e, o are of the implicit type var. The compiler can determine its type so that these two lines are equivalent:
List<int> eo = Enumerable.Range(1, 100).ToList();
Then, to find the coefficients / evens directly to the list type:
List<int> o = eo.Where(num => num%2!=0).ToList(); List<int> e = eo.Where(num => num%2==0).ToList();
And for printing, this is indicated in my source code.
Jonh
source share