Q&A with Jason Bock: What's New with Concurrent Programming in .NET

From new features like async return types and advice on when to use the latest structures, here's Microsoft MVP Jason Bock's top tips for developers currently working with concurrent and asynchronous programming.

Asynchronous and concurrent programming can quickly get complicated. So we asked Microsoft MVP Jason Bock, who's speaking on this topic at the upcoming Visual Studio Live! Boston conference in June, to give share some of his top tips and advice for what's new in this area, and what developers should (and shouldn't) do in their projects to help them go more smoothly.

What are the biggest improvements that Microsoft has made toward concurrent programming in .NET in the last year or so?
It's a minor addition, but in C# 7.0 a new feature was added called "Generalized async return types." This means that you're no longer limited to using Task or Task<T> as the return type for an asynchronous method. As long as the return type satisfies the asynchronous pattern, you're good. The ValueTask type, which is a value type, is a concrete example of this, though you're able to create your own custom Task type if you want. Using this new ValueTask, you can avoid memory allocations, which can help in addressing performance issues.

What do developers not quite understand about asynchronous programming/what makes it harder to get into than it should?
The biggest difficulty I see with some developers is associating async/await with threads.

"There's still the notion out there that an asynchronous method automatically spawns a new thread, and that is not the case."

Jason Bock, MVP (C#), Practice Lead, Magenic

There's still the notion out there that an asynchronous method automatically spawns a new thread, and that is not the case. One of the main advantages of using asynchronous methods is with I/O-based code. By doing an await, you can let a thread be reused to do other work while the I/O is in flight.

What are the top data structures and/or libraries that you recommend for asynchronous/concurrent programming?
There are two NuGet packages called System.Collections.Concurrent and System.Collections.Immutable. The former provides collections that allow a developer to use a collection (e.g. ConcurrentBag or ConcurrentDictionary) in a concurrent fashion safely. Therefore, the developer doesn't need to do their own locking mechanisms to use the collection. Immutable collections allow developers to share collections safely because updates are only seen by the code that made the update.

In your session on this topic at the upcoming Visual Studio Live! Boston conference, one topic you're diving into is "immutable structures." Can you offer a quick glimpse into why these are so important when working with concurrent programming? What role do they play?
The main advantage with immutable data types is that you can share them across multiple tasks without worrying about synchronizing their contents. By their definition, they're immutable, so if a task decides to "change" an immutable object, it will get a new version of that object. If another task has a reference to the original immutable object, it doesn't change. This model makes it much easier to reason about the state of an object in concurrent scenarios.

What's your top tip when it comes to this type of programming?
Developers should take advantage of features within C# to make it easier to write immutable structures. This means defining fields with the readonly keyword and properties that only have getters (not even private setters). Also, C# now allows you to specify a method parameter with the in keyword, which means that reference cannot be changed.

What's your number one tip for what NOT to do? Is there anything that you want to warn against?
Avoid having void as a return type for asynchronous methods at all costs. The only time it's valid to do this is with an event handler. Otherwise, asynchronous methods should always return a Task type. There are a lot of problems that go away as long as you don't do this.

What's one of your favorite resources (aside from your Visual Studio Live! session) for learning more about this topic?
There's two books that I highly recommend: Stephen Cleary's "Concurrency in C# Cookbook" and Joe Duffy's "Concurrent Programming on Windows." They both provide valuable, deep insight into concurrent programming in .NET.

Learn more about this topic from Jason plus much, much more at Visual Studio Live! Boston in June 2018.

About the Author

Becky Nagel is the vice president of Web & Digital Strategy for 1105's Converge360 Group, where she oversees the front-end Web team and deals with all aspects of digital projects at the company, including launching and running the group's popular virtual summit and Coffee talk series . She an experienced tech journalist (20 years), and before her current position, was the editorial director of the group's sites. A few years ago she gave a talk at a leading technical publishers conference about how changes in Web browser technology would impact online advertising for publishers. Follow her on twitter @beckynagel.

comments powered by Disqus


Subscribe on YouTube

Upcoming Events