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.

Read-only properties

Read-only properties

- [Instructor] There seems to be contrary goals for functional types. We need changeable types to get work done, but we want immutable types for good reasons too. One misconception about immutability is that while the type itself is not mutable, we need to be able to change the data too. So don't think of it as forbidding change, instead it's about how to handle change. Here's a diagram to help illustrate. While we instantiate the DateTime type, the properties are initialized. Here you can see the years 2025, the month is 10, and the day is 24. Operations that modify the object properties are purposefully created as methods on the type that return a new instance. In this example, I'm calling the AddDays methods. A new instance of the type is created with the modified values. Think of it this way. The type os always immutable at rest. Over the years, the programmers have found some good design principles for creating…

Contents