From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Don't mutate input arguments

Don't mutate input arguments

- [Narrator] Here's one way that we can solve the problem of mutating shared state. Remove that class level variable, and instead pass in the integer value, the counter value, in as a parameter. So that's what I'm doing here. I've renamed these methods. It's now called increment by five and increment by two. I no longer have the shared integer value. I'm passing in an integer value here and doing my calculation and returning the new number in both these functions. And then up here, I'm creating a new integer variable starting at 10, I'm passing that in to increment by two, and then I'm starting that here in this new variable, so this will be a value of 12. And if I pass 12 in here to increment by five, I'll get back 17. And this works, I've made these pure functions. We have to remember though, you don't want to mutate the input arguments. Otherwise you've got a side effect. And I'm not doing that here, but let me…

Contents