Complete the code to read all text asynchronously from a file.
string content = await File.[1]("example.txt");
The method ReadAllTextAsync reads the entire content of a file asynchronously as a string.
Complete the code to write text asynchronously to a file.
await File.[1]("output.txt", "Hello World!");
The method WriteAllTextAsync writes the entire string content asynchronously to a file.
Fix the error in the async method declaration.
public async [1] ReadFileAsync(string path) { return await File.ReadAllTextAsync(path); }
The method returns a Task<string> because it is asynchronous and returns a string result.
Fill both blanks to create a dictionary with file names as keys and their sizes as values asynchronously.
var fileSizes = new Dictionary<string, long>(); foreach (var file in files) { var info = new FileInfo(file); fileSizes[[1]] = await Task.Run(() => info.[2]); }
The key is the file path stored in file, and the size is obtained from the Length property of FileInfo.
Fill all three blanks to asynchronously read multiple files and store their contents in a dictionary with file names as keys.
var contents = new Dictionary<string, string>(); foreach (var [1] in fileList) { contents[[2]] = await File.[3]([2]); }
The loop variable is fileName, used as the dictionary key and passed to ReadAllTextAsync to read the file content asynchronously.