From the course: Advanced C#: Functional Programming Patterns
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Rewrite statements as expressions - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Rewrite statements as expressions
- [Instructor] Let's look at how to refactor statement base code and convert it into expression base code. Before I show you the code, let's talk about two of the types I'm using in this demo. I have an enumeration called standard colors with these color values, and I have a product class, this is a immutable type. It's got a read only retail price and product name property and it has a single constructor. What I'm doing on line 14 is declaring an instance of the product of, I have a product called microphone, that's retailing for $200, trying that there, and then I'm going to call these two methods, get product price, and get product price by expression. And here's how we would write this code with if statements. I have a get product price method that returns the calculated price of the product as a decimal, it takes the product itself. The number of products that the customer is buying, and a Boolean that determines…