

C#: Make your delegates asynchronous from synchronous delegates
source link: https://anthonygiretti.com/2021/05/01/c-make-your-delegates-asynchronous-from-synchronous-delegates/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C#: Make your delegates asynchronous from synchronous delegates
Introduction
I wanted to write this post because I realized that a lot of developers have difficulty writing asynchronous delegates. I often see synchronous delegates in code review that could be transformed. So in this post I will show you how to proceede without however going over the usefulness of asynchronism, this is not the purpose of this post.
Synchronous delegates
There are 4 types of delegates in C #, you have delegates who:
- Return a result
- Do not return a result
but also delegates who:
- Take one or more parameters
- Do not take any parameters.
So that’s 4 ways to write a delegate, now let’s see what it looks like:
- Delegate that doesn’t take any parameter and doesn’t return any value: Action
- Delegate that takes one or several parameter and doesn’t return any value: Action<Tin>
- Delegate that doesn’t take any parameter and returns a value: Func<Tout>
- Delegate that doesn’t take any parameter and returns a value: Func<Tin, Tout>
Let’s look at a concrete example how this is implemented in practice:
using System;
namespace ConsoleApp1 { public class DoWork { // Action public void DoWorkA(Action action) { action(); }
// Action<Tin> public void DoWorkB(Action<string> action) { action("DoWorkB executed"); }
// Func<Tout> public bool DoWorkC(Func<bool> func) { return func(); }
// Func<Tin, Tout> public bool DoWorkD(Func<string, bool> func) { return func("DoWorkD executed"); } } class Program { static void Main(string[] args) { // Synchronous var doWork = new DoWork();
doWork.DoWorkA(() => Console.WriteLine("DoWorkA executed"));
doWork.DoWorkB((string param) => Console.WriteLine(param));
doWork.DoWorkC(() => { Console.WriteLine("DoWorkC executed"); return true; });
doWork.DoWorkD((string param) => { Console.WriteLine(param); return true; }); } } }
Synchronous to asynchronous delegates
The rule for obtaining a synchronous delegate is simple:
It doesn’t matter if this is an Action or a Func, the asynchronous version will always be a Func returning a Task which gives :
- Asynchronous Delegate that doesn’t take any parameter and doesn’t return any value: Func<Task>
- Asynchronous Delegate that takes one or several parameter and doesn’t return any value: Func<Tin, Task>
- Asynchronous Delegate that doesn’t take any parameter and returns a value: Func<Task<Tout>>
- Asynchronous Delegate that doesn’t take any parameter and returns a value: Func<Tin, Task<Tout>>
Example:
using System; using System.Threading.Tasks;
namespace ConsoleApp1 { public class DoWorkAsync { // Func<Task> public async Task DoWorkAAsync(Func<Task> task) { await task(); } // Func<Tin, Task> public async Task DoWorkBAsync(Func<string, Task> task) { await task("DoWorkBAsync executed"); }
// Func<Task<Tout>> public async Task<bool> DoWorkCAsync(Func<Task<bool>> task) { return await task(); }
// Func<Tin, Task<Tout>> public async Task<bool> DoWorkDAsync(Func<string, Task<bool>> task) { return await task("DoWorkDAsync executed"); } } class Program { static async Task Main(string[] args) { // Asynchronous Console.WriteLine();
var doWorkAsync = new DoWorkAsync();
await doWorkAsync.DoWorkAAsync(async () => await Task.Run(() => Console.WriteLine("DoWorkAAsync executed")) );
await doWorkAsync.DoWorkBAsync(async (string param) => await Task.Run(() => Console.WriteLine(param)) );
await doWorkAsync.DoWorkCAsync(async () => { await Task.Run(() => Console.WriteLine("DoWorkCAsync executed")); return true; });
await doWorkAsync.DoWorkDAsync(async (string param) => { await Task.Run(() => Console.WriteLine(param)); return true; }); } } }
That’s it!
I hope this tutorial helped you to understand how to transform your delegates into asynchronous delegates! Happy programming
Like this:
Related posts
Recommend
-
108
Have you ever wondered why you have to wrestle with CompletableFutures instead of “just writing code” like in the old days? Is it only for performance? Or maybe it’s just a fashion? Let’s find out…
-
26
Introduction This article is the second part of a series on using Python for developing asynchronous web applications. The first part provides a more in-depth coverage of concurrency in Python and asy...
-
17
Comparing synchronous and asynchronous Access to Postgres Quarkus supports imperative as well as reactive programming styles. In this article I compare access times to
-
9
Javascript: await looks synchronous but it is asynchronous Dec 21, 2016 • Yehonathan Sharvit What is going to be the output about this javascript snippet? Will the
-
11
-
21
Callbacks, synchronous and asynchronous Callbacks, synchronous and asynchronous by havoc Here are two guidelines for designing APIs that use callbacks, to add to my inadvertent col...
-
11
As the world has moved toward remote work, many companies are starting to move towards asynchronous standup meetings as well. Unfortunately, asynchronous standups...
-
10
NAKIVO Blog > Data Protection > Synchronous vs. Asynchronous Replication Strategy...
-
7
XMLHttpRequest supports both synchronous and asynchronous communications. In general, however, asynchronous requests should be preferr...
-
8
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK