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

Using statement for resource cleanup 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 ensure the StreamReader is properly disposed after use.

C Sharp (C#)
using ([1] reader = new StreamReader("file.txt"))
{
    string content = reader.ReadToEnd();
    Console.WriteLine(content);
}
Drag options to blanks, or click blank then click option'
AStreamReader
BFileStream
CMemoryStream
DBinaryReader
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type that does not implement IDisposable.
Using a stream type that reads bytes instead of characters.
2fill in blank
medium

Complete the code to open a file stream with automatic cleanup.

C Sharp (C#)
using ([1] fileStream = new FileStream("data.bin", FileMode.Open))
{
    // Read data
}
Drag options to blanks, or click blank then click option'
AFileStream
BStreamReader
CMemoryStream
DBinaryReader
Attempts:
3 left
💡 Hint
Common Mistakes
Using StreamReader when binary access is needed.
Using MemoryStream which is not connected to a file.
3fill in blank
hard

Fix the error in the using statement to properly dispose the resource.

C Sharp (C#)
using [1] = new StreamWriter("output.txt");
writer.WriteLine("Hello World");
Drag options to blanks, or click blank then click option'
AStreamWriter
BStreamWriter writer
Cvar writer
Dwriter
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the variable name.
Using only the type without variable name.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores file lengths for files larger than 1000 bytes.

C Sharp (C#)
var fileLengths = new Dictionary<string, long>()
{
    { [1], [2] }
};
Drag options to blanks, or click blank then click option'
A"file.txt"
Bnew FileInfo("file.txt").Length
Cnew FileInfo("file.txt").Name
D"length"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the file name property incorrectly.
Using a string literal for the file length.
5fill in blank
hard

Fill all three blanks to create a using statement that reads all lines from a file and prints them.

C Sharp (C#)
using ([1] reader = new [2]("log.txt"))
{
    string line;
    while ((line = reader.[3]()) != null)
    {
        Console.WriteLine(line);
    }
}
Drag options to blanks, or click blank then click option'
AStreamReader
BReadLine
CStreamWriter
DFileStream
Attempts:
3 left
💡 Hint
Common Mistakes
Using StreamWriter instead of StreamReader.
Using FileStream which reads bytes, not lines.
Using a method other than ReadLine to read lines.