From the course: Advanced C#: Functional Programming Patterns
Unlock this course with a free trial
Join today to access over 24,500 courses taught by industry experts.
Rewrite IF statement with expression - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Rewrite IF statement with expression
Let's look at how to refactor statement-based code and convert it into expression-based code. Before I show you the code, let's talk about the type that I'm using in this example. It's down here near the bottom of this file. It is the product class. It's an immutable type. It has a read-only retail price property, a read-only product name property, it has a constructor for setting those two properties, and it has an AdjustPrice() method for changing the price of the product. I'm not using the AdjustPrice() in this example. Why am I using this? It's up here in this section. We're going to talk about how to refactor if statements. What I have here is two methods, GetProductPrice(), which has statement-based code. And then I have the GetProductPriceByExpression() which has the expression-based code. The way I'm calling these is up in this method, the RunIf() method. Is on line 20, I'm instantiating an instance of the product class. The product name is 'Microphone', the retail price is…
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)
-
-
-
-
-