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

StreamReader and StreamWriter 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 open a file for reading using StreamReader.

C Sharp (C#)
using System.IO;

var reader = new StreamReader([1]);
string content = reader.ReadToEnd();
reader.Close();
Drag options to blanks, or click blank then click option'
AFile.txt
Bfile.txt
C"file.txt"
Dfile
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the file name in quotes.
Using incorrect capitalization without quotes.
2fill in blank
medium

Complete the code to write text to a file using StreamWriter.

C Sharp (C#)
using System.IO;

using (var writer = new StreamWriter([1]))
{
    writer.WriteLine("Hello World");
}
Drag options to blanks, or click blank then click option'
Afile
Bfile.txt
CFile.txt
D"file.txt"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the file name without quotes.
Using incorrect capitalization without quotes.
3fill in blank
hard

Fix the error in the code to properly read a line from a file.

C Sharp (C#)
using System.IO;

var reader = new StreamReader("data.txt");
string line = reader.[1]();
reader.Close();
Drag options to blanks, or click blank then click option'
Areadline
BReadLine
CreadLine
DReadline
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters in method names.
Incorrect capitalization causing method not found errors.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.

C Sharp (C#)
var words = new List<string> { "apple", "cat", "banana", "dog" };
var lengths = words.Where(w => w.Length > 3).ToDictionary([1], [2]);
Drag options to blanks, or click blank then click option'
Aw => w
Bw => w.Length
Cw => w.Length > 3
Dw => w.ToUpper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition as a key or value selector.
Using ToUpper() instead of length for values.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 4 characters.

C Sharp (C#)
var words = new List<string> { "apple", "cat", "banana", "dog" };
var result = words.Where(w => w.Length > [1]).ToDictionary([2], [3]);
Drag options to blanks, or click blank then click option'
A3
B4
Cw => w.ToUpper()
Dw => w
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong length filter number.
Swapping keys and values in ToDictionary.