Complete the code to read all lines from a file into a string array.
string[] lines = System.IO.File.[1]("example.txt");
The method ReadAllLines reads all lines from a text file and returns them as a string array.
Complete the code to read the entire content of a file as a single string.
string content = System.IO.File.[1]("example.txt");
The method ReadAllText reads the entire file content as one string.
Fix the error in the code to open a file and read its first line using StreamReader.
using var reader = new System.IO.StreamReader("example.txt"); string firstLine = reader.[1]();
The ReadLine method reads the next line from the StreamReader.
Fill both blanks to read all lines from a file and print only lines longer than 5 characters.
string[] lines = System.IO.File.[1]("example.txt"); foreach (var line in lines) { if (line.[2] > 5) { Console.WriteLine(line); } }
ReadAllLines reads all lines into an array. Length gives the number of characters in a string.
Fill all three blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.
var words = new List<string> { "apple", "cat", "banana", "dog" };
var wordLengths = words.ToDictionary([1] => [2], [3] => [3].Length);
var filtered = wordLengths.Where(kv => kv.Value > 3).ToDictionary(kv => kv.Key, kv => kv.Value);We use a lambda with a variable name for the key selector and value selector. Both use the same variable w. The key is w and the value is w.Length.