What is an asynchronous programming in terms of c#?


async keyword in a method signature, the method is not an asynchronous method. Instead, it is a synchronous method that returns a Task


Download 27.19 Kb.
bet2/6
Sana31.03.2023
Hajmi27.19 Kb.
#1313565
1   2   3   4   5   6
Bog'liq
Asynchronous

async keyword in a method signature, the method is not an asynchronous method. Instead, it is a synchronous method that returns a Task object.
In the case of public Task GetAll(), the method signature declares a synchronous method named GetAll that returns a Task object.
The implementation of the method will depend on the specific requirements of the application and the type of operation being performed. However, the method should be designed to perform its operation synchronously, without any await calls or other asynchronous constructs.

It does not create state machine right?


That's correct. Since the method public Task GetAll() is not marked with the async keyword, it does not use any await expressions or other asynchronous constructs that would require the C# compiler to generate a state machine.
Therefore, this method does not create a state machine, and it is implemented as a regular synchronous method that returns a Task object to allow callers to use it in an asynchronous context.
In contrast, when you use the async keyword with a method signature, the C# compiler generates a state machine behind the scenes to handle the asynchronous execution flow of the method.

Let's say I have a repository class. I have public async Task GetAll() method in there. Let's say I have service class as well. It calls that repository using dependency injection. It has method: public async Task GetAllData() { return await _repository.GetAll(); } Here as you can see, I awaited the data 2 times. I guess, I created state machine 2 times as well


Yes, you are correct. In the example you provided, you have used the await keyword twice, which creates two separate state machines:

  1. The first state machine is created in the GetAllData method, which is marked with the async keyword and uses the await keyword to asynchronously call the _repository.GetAll() method.

  2. The second state machine is created in the
    Download 27.19 Kb.

    Do'stlaringiz bilan baham:
1   2   3   4   5   6




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling