Skip to content

Instantly share code, notes, and snippets.

@pablocar80
Last active October 9, 2019 17:21
Show Gist options
  • Save pablocar80/b5a314cceda6775f028547ee7f909924 to your computer and use it in GitHub Desktop.
Save pablocar80/b5a314cceda6775f028547ee7f909924 to your computer and use it in GitHub Desktop.
converting synchronous function
class Test
{
// Synchronous version
public string MySyncFunction()
{
string text = File.ReadAllText("myfile.txt");
return text;
}
// Asynchronous version. Notice ‘async Task’ and ‘await’.
public async Task<string> MyAsyncFunction()
{
string text = await File.ReadAllTextAsync("myfile.txt");
return text;
}
// Synchronous with async signature. Notice ‘Task.FromResult’ and missing ‘async’.
public Task<string> MySyncFunctionWithAsyncSignature()
{
string text = File.ReadAllText("myfile.txt");
return Task.FromResult(text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment