Book Image

Hands-On Parallel Programming with C# 8 and .NET Core 3

By : Shakti Tanwar
Book Image

Hands-On Parallel Programming with C# 8 and .NET Core 3

By: Shakti Tanwar

Overview of this book

In today’s world, every CPU has a multi-core processor. However, unless your application has implemented parallel programming, it will fail to utilize the hardware’s full processing capacity. This book will show you how to write modern software on the optimized and high-performing .NET Core 3 framework using C# 8. Hands-On Parallel Programming with C# 8 and .NET Core 3 covers how to build multithreaded, concurrent, and optimized applications that harness the power of multi-core processors. Once you’ve understood the fundamentals of threading and concurrency, you’ll gain insights into the data structure in .NET Core that supports parallelism. The book will then help you perform asynchronous programming in C# and diagnose and debug parallel code effectively. You’ll also get to grips with the new Kestrel server and understand the difference between the IIS and Kestrel operating models. Finally, you’ll learn best practices such as test-driven development, and run unit tests on your parallel code. By the end of the book, you’ll have developed a deep understanding of the core concepts of concurrency and asynchrony to create responsive applications that are not CPU-intensive.
Table of Contents (22 chapters)
Free Chapter
1
Section 1: Fundamentals of Threading, Multitasking, and Asynchrony
6
Section 2: Data Structures that Support Parallelism in .NET Core
10
Section 3: Asynchronous Programming Using C#
13
Section 4: Debugging, Diagnostics, and Unit Testing for Async Code
16
Section 5: Parallel Programming Feature Additions to .NET Core

Async delegates and lambda expressions

We can use the async keyword to create asynchronous delegates and lambda expressions as well.

Here is a synchronous delegate that returns the square of a number:

Func<int, int> square = (x) => {return x * x;};

We can make the preceding delegate asynchronous by appending the async keyword, as follows:

Func<int, Task<int>> square =async (x) => {return x * x;};

Similarly, lambda expressions can be converted, as follows:

Func<int, Task<int>> square =async (x) => x * x;

Asynchronous methods work in a chain. Once you have made any one method asynchronous, then all methods that call that method need to be converted to being asynchronous as well, thus creating a long chain of asynchronous methods.