From the course: Advanced C#: Functional Programming Patterns
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Rewrite FOREACH with expression - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Rewrite FOREACH with expression
This is what we're going to attempt to refactor in this example. It's a foreach loop. In fact, there's some nested foreach loops and there's also an if statement in here. So the idea behind this is I want to take a list of numbers, and I want to look through all of those numbers and find numbers that end with some specific digits. In our case, they end with either 14, 23, 37 or 49. So I'm passing, in this example, I'm passing in two lists of ints, all the numbers, and the target numbers I'm looking for. My code starts out by creating a list of int called matching numbers. And then I've got the foreach statement here that's going through all the numbers. And then the next foreach is going through all the target numbers. And then it's doing an if statement here to determine if it meets my target, number % 100 == target. And if that's the case, then we add it to this list. Here's how we would do it with an expression. Now this is all one expression. So we're returning the results of this…
Contents
-
-
-
-
-
-
-
-
(Locked)
Why expressions are better for functional programs2m 20s
-
(Locked)
Some examples of C# expressions2m 16s
-
(Locked)
Rewrite IF statement with expression4m 41s
-
(Locked)
Rewrite SWITCH statement with expression3m 4s
-
(Locked)
Rewrite FOREACH with expression1m 45s
-
(Locked)
IsPrime function as an expression1m 58s
-
(Locked)
-
-
-
-
-