News

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 serves as vice president of AI for 1105 Media specializing in developing media, events and training for companies around AI and generative AI technology. She also regularly writes and reports on AI news, and is the founding editor of PureAI.com. She's the author of "ChatGPT Prompt 101 Guide for Business Users" and other popular AI resources with a real-world business perspective. She regularly speaks, writes and develops content around AI, generative AI and other business tech. Find her on X/Twitter @beckynagel.

comments powered by Disqus

Featured

  • Final .NET 9 Preview Ships with Go-Live License

    Visual Studio developers can now download the SDK for .NET 9 Release Candidate 2 with a go-live license, meaning devs get Microsoft support for production applications even before the framework reaches general availability next month.

  • Upcycle Your Old Laptops into a Kubernetes Cluster

    Learn about Windows-to-Linux conversions and how to break and fix cloud containers -- all while helping to save the world from e-waste with some "sheer geeky fun."

  • Visual Studio's Future: Live Help to 'AI-ify Your App'

    Visual Studio guru Mads Kristensen provided a peek into the IDE's AI future, explaining how while in live-coding it will identify opportunities for your own app to use AI to your advantage.

  • Python AI Updates Highlight New VS Code Release

    Millions of Python developers using VS Code find updated data science functionality in the new release of version 1.94, the September 2024 edition of Microsoft's open-source-based editor.

Subscribe on YouTube