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.

Read-only properties

Read-only properties

We will look at how to create read-only properties, the best way to do that in a C# type. We'll also look at how to create a constructor to initialize those properties. For this example, I have created a color class that simulates the old-school RGB colors types that we would see on our old-school monitors. So there's a red and a green and blue channel, and the data that's stored in those channels is one byte from 0 to 255. On line 8, I created my class and I made it sealed. This is a principle you might want to consider for your type. And then I have properties, orange and purple I'm only using in this first demo, they won't be in the final type, but it allows me to show you multiple ways of setting up a property. The first thing I want to mention is don't write your properties like this. In the earlier days of C#, this was the way we did it, but this is not useful for immutable types. It's a public getter and a private setter. This means that outside of the type, you can't set the…

Contents