3 ms·
pet peeve, but at least the C# examples (and probably others as well) can IMHO be written more elegantly: Example 1: static IEnumerable<int> GetEvenNumber
by useerup 7y ago
pet peeve, but at least the C# examples (and probably others as well) can IMHO be written more elegantly:
Example 1:
static IEnumerable<int> GetEvenNumbers(IEnumerable<int> l) => l.Where(x => x%2 == 0)
Example 2:
static IEnumerable<FileInfo> GetFilesGreaterThan(int size,string directory)
{
DirectoryInfo dirInfo = new DirectoryInfo(directory);
return dirInfo.GetFiles().Where(x => x.Length >= size)
}
Example 3:
static void SolveProblem()
{
var results =
from a in Enumerable.Range(0,9)
from b in Enumerable.Range(0,9)
from c in Enumerable.Range(0,9)
from d in Enumerable.Range(0,9)
where (1000*a + 100*b + 10*c + d)*4 ==
(1000*d + 100*c + 10*b + a)
&& a != 0
select new {A=a,B=b,C=c,D=d};
foreach(var r in results) {
Console.WriteLine($"A={r.A} B={r.B} C={r.C} D={r.D}");
}
}
One important point is not to confuse List and IEnumerable of .NET. What is called "lists" in many languages are actually more akin to IEnumerable in .NET