Result
Before reading this part, please read asynchronous and await. They are curtial, to understand the following.
What is the Result keyword in dotnet
In dotnet, the Result
keyword is highly important. It is used to get the result of a Task. The main difference to await is, that Result
is blocking the whole process before running the rest.
This is important to know, as it can lead to deadlocks, when not used correctly. A deadlock happens, when one process is waiting to finish another process that needs the first one to be done to proceed.
That's why it is not recommended to use Result
much.
In dotnet it is mostly and nearly only used in Unit Tests.
Here is an example:
public async Task RunningMethod(this List<int> integers)
{
int result = integers.GetFirstNum().Result;
Console.WriteLine(result);
}
public async Task<int> GetFirstNum(this List<int> integers)
{
await Task.Delay(1000); // Delays the task for 1 second
return integers.First();
}