Complete the code to ensure the StreamReader is properly disposed after use.
using ([1] reader = new StreamReader("file.txt")) { string content = reader.ReadToEnd(); Console.WriteLine(content); }
The using statement requires a disposable resource. Here, StreamReader is the correct type to read text files and be disposed automatically.
Complete the code to open a file stream with automatic cleanup.
using ([1] fileStream = new FileStream("data.bin", FileMode.Open)) { // Read data }
FileStream is used to open a file for reading or writing bytes and supports disposal with using.
Fix the error in the using statement to properly dispose the resource.
using [1] = new StreamWriter("output.txt"); writer.WriteLine("Hello World");
In C# 8.0 and later, the using declaration syntax requires the variable to be declared with var or explicit type and variable name. Here, var writer is correct.
Fill both blanks to create a dictionary comprehension that stores file lengths for files larger than 1000 bytes.
var fileLengths = new Dictionary<string, long>()
{
{ [1], [2] }
};The dictionary key is the file name as a string, and the value is the file length obtained from FileInfo.Length.
Fill all three blanks to create a using statement that reads all lines from a file and prints them.
using ([1] reader = new [2]("log.txt")) { string line; while ((line = reader.[3]()) != null) { Console.WriteLine(line); } }
StreamReader is used to read text lines from a file. The method ReadLine() reads one line at a time.