Async Feature in C# 5.0
async & await two keywords introduce in C# 5.0. Which allowed you to write asynchronous Programming in easily and proper way. Before this writing an asynchronous Programming is complicated with callback function.
We need to use combination of both keywords to write an asynchronous code. We can use await operator with more than one expression in async method. We can say that both keywords in C# are the heart of async programming. A method whis is defined by using with these keywords as reffered and you can say identified to as async methods.
Following areas where asynchronous programming help us
|Area||Supporting APIs contain async methods|
|Working with files|
|Working with images|
|WCF programming||Synchronous and Asynchronous Operations|
public async Task<IEnumerable<File>> GetFileList()
HttpClient fileClient = new HttpClient();
Uri fileAddress = new Uri("http://firstcrazydeveloper.com/");
fileClient.BaseAddress = fileAddress;
HttpResponseMessage getFileListresponse = await fileClient.GetAsync("fileService/File/FileList");
var fileList = await getFileListresponse.Content.ReadAsAsync<IEnumerable<File>>();
Note for Async Feature
- The method signature includes an Async or async modifier.
- The name of an async method, by convention, ends with an "Async" suffix.
- The method usually includes at least one await expression, which marks a point where the method can't continue until the awaited asynchronous operation is complete. In the meantime, the method is suspended, and control returns to the method's caller. The next section of this topic illustrates what happens at the suspension point.
The return type is one of the following types:
if your method has a return statement in which the operand has type TResult.
- Task if your method has no return statement or has a return statement with no operand.