0
0
C Sharp (C#)programming~10 mins

Async file reading and writing in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read all text asynchronously from a file.

C Sharp (C#)
string content = await File.[1]("example.txt");
Drag options to blanks, or click blank then click option'
AReadFileAsync
BReadTextAsync
CReadAllTextAsync
DReadAsyncText
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the File class.
Confusing synchronous and asynchronous methods.
2fill in blank
medium

Complete the code to write text asynchronously to a file.

C Sharp (C#)
await File.[1]("output.txt", "Hello World!");
Drag options to blanks, or click blank then click option'
AWriteAllTextAsync
BWriteTextAsync
CWriteFileAsync
DWriteAsyncText
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the File class.
Confusing synchronous and asynchronous methods.
3fill in blank
hard

Fix the error in the async method declaration.

C Sharp (C#)
public async [1] ReadFileAsync(string path)
{
    return await File.ReadAllTextAsync(path);
}
Drag options to blanks, or click blank then click option'
Avoid
BTask<string>
Casync
DTask
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as return type for async methods that return a value.
Using 'Task' instead of 'Task' when returning a string.
4fill in blank
hard

Fill both blanks to create a dictionary with file names as keys and their sizes as values asynchronously.

C Sharp (C#)
var fileSizes = new Dictionary<string, long>();
foreach (var file in files)
{
    var info = new FileInfo(file);
    fileSizes[[1]] = await Task.Run(() => info.[2]);
}
Drag options to blanks, or click blank then click option'
Afile
BLength
CName
DSize
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong property name for file size.
Using the wrong variable for the dictionary key.
5fill in blank
hard

Fill all three blanks to asynchronously read multiple files and store their contents in a dictionary with file names as keys.

C Sharp (C#)
var contents = new Dictionary<string, string>();
foreach (var [1] in fileList)
{
    contents[[2]] = await File.[3]([2]);
}
Drag options to blanks, or click blank then click option'
AfileName
BReadAllTextAsync
Cfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names.
Using a synchronous method instead of an async one.