From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

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

Use helper methods to create new instance

Use helper methods to create new instance - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Use helper methods to create new instance

- [Instructor] So this immutable type, if I want to change any of the properties on it, all my properties are read only. So the correct way of doing this in immutable types is to create helper methods that take the modified value, create a new instance of the type, apply the modified value, and return the new instance, so you never affect the original immutable instance. So here's how I'm going to do that. I'm going to create two methods, one called lighten and one called darken. And you notice that they return a color type. So they'll return the new instance. And the idea here is I want to lighten all three color channels by a certain byte value, or I want to darken it by the same value. So what I'm doing here is I'm calculating a new red value here based on this clamp method. So the idea is if I lighten it, I don't want the red channel to ever be higher than 255. So I'm clamping it. So I'm saying here's the red, the…

Contents