Last active
October 9, 2019 17:21
-
-
Save pablocar80/b5a314cceda6775f028547ee7f909924 to your computer and use it in GitHub Desktop.
converting synchronous function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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