From the course: Ten Tips for the C# Developer

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use the ImmutableList collection

Use the ImmutableList collection - C# Tutorial

From the course: Ten Tips for the C# Developer

Start my 1-month free trial

Use the ImmutableList collection

- [Instructor] Let's keep going with this theme of thread-safe code. On the last tip, I showed you how to take a RayPoint type and convert it into an ImmutableRayPoint type and then I did that by creating read-only properties and a parametrized constructor and I also created some special helper methods. One of the problems with the code that I wrote in the last example is that when you call GetShape, I'm returning a list of T, so the type ImmutableRayPoint is thread safe but the list of T that comes from the .NET framework is not thread safe so what we're going to do is modify our code so we're working with a thread-safe list. And there are many different collection classes in .NET that are thread safe. The one we're looking at for this example is called ImmutableList. Let's start by looking at the old code. Here in GetShape, I'm creating a list of T and populating it with list data. Now, because list of T is not…

Contents