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

Reading text files 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 lines from a file into a string array.

C Sharp (C#)
string[] lines = System.IO.File.[1]("example.txt");
Drag options to blanks, or click blank then click option'
AGetLines
BReadAllLines
CReadFile
DReadText
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that does not exist like ReadText or GetLines.
Confusing ReadAllLines with ReadAllText which returns a single string.
2fill in blank
medium

Complete the code to read the entire content of a file as a single string.

C Sharp (C#)
string content = System.IO.File.[1]("example.txt");
Drag options to blanks, or click blank then click option'
AReadAllText
BReadAllLines
CReadTextFile
DReadFileContent
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadAllLines which returns an array of strings.
Using non-existent methods like ReadTextFile.
3fill in blank
hard

Fix the error in the code to open a file and read its first line using StreamReader.

C Sharp (C#)
using var reader = new System.IO.StreamReader("example.txt");
string firstLine = reader.[1]();
Drag options to blanks, or click blank then click option'
AReadAllLines
BRead
CReadToEnd
DReadLine
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadAllLines which is not a StreamReader method.
Using ReadToEnd which reads the whole file.
4fill in blank
hard

Fill both blanks to read all lines from a file and print only lines longer than 5 characters.

C Sharp (C#)
string[] lines = System.IO.File.[1]("example.txt");
foreach (var line in lines)
{
    if (line.[2] > 5)
    {
        Console.WriteLine(line);
    }
}
Drag options to blanks, or click blank then click option'
AReadAllLines
BLength
CCount
DReadAllText
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadAllText which returns a single string, not an array.
Using Count which is not a string property.
5fill in blank
hard

Fill all three blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.

C Sharp (C#)
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);
Drag options to blanks, or click blank then click option'
Aword
Bw
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for key and value selectors.
Not using the Length property for the value.