So far every program has had its data written into it. Console.ReadLine() reads the next line of standard input, the stream of text a program is given. In a terminal, that's what the user types. Here you fill in the input box before pressing Run, because a running program can't stop and wait for you. The two work the same way as far as your code is concerned.
Input is always text
ReadLine gives you a string, even when the text looks like a number. Change the values in the input box and run this again.
string name = Console.ReadLine();
string minutes = Console.ReadLine();
Console.WriteLine($"Hello, {name}");
Console.WriteLine(minutes + 5);Check your prediction
Hello, Ada
255"25" + 5 joins text to make 255. The next lesson converts text to numbers. For now, keep in mind that you can't trust input to be what you expected.
The end of input is null
When the input runs out, ReadLine returns null, a special value that means "no object here". A null string is different from an empty one. An empty line gives you "", which is a real string with a Length of 0. A null is no string at all, and using it as though it were one throws an exception.
string first = Console.ReadLine();
string second = Console.ReadLine();
Console.WriteLine(first.Length);
Console.WriteLine(second is null);
Console.WriteLine(second.Length);Check your prediction
9
True
Unhandled exception. System.NullReferenceExceptionNullReferenceException is one of the most common crashes in C#. So C# gives you a way to have the compiler find them first.
Let the compiler look for null
#nullable enable at the top of a file turns on nullable checking. From then on, string means "definitely a string", and string? means "a string or null". The compiler tracks which is which and warns you when you use a value that might be null. A new project made with dotnet new has this setting on everywhere, so you'll see these warnings in real work. This browser runtime has no project file, so you add the line yourself.
#nullable enable
string? line = Console.ReadLine();
Console.WriteLine(line.Length);Check your prediction
warning CS8602: Dereference of a possibly null reference.
Unhandled exception. System.NullReferenceExceptionOnce you test for null, the compiler knows the value is safe in that branch and the warning goes away. For a fallback value, ?? gives the right side when the left is null.
#nullable enable
string? line = Console.ReadLine();
if (line is not null)
{
Console.WriteLine(line.Length);
}
string next = Console.ReadLine() ?? "(no more input)";
Console.WriteLine(next);Check your prediction
3
(no more input)Reading every line
You usually don't know how many lines there are. This loop is the standard shape: read a line, stop if it's null, and otherwise handle it. The assignment sits inside the condition, so reading and testing happen together.
#nullable enable
int count = 0;
string? line;
while ((line = Console.ReadLine()) is not null)
{
count++;
Console.WriteLine($"{count}: [{line}]");
}
Console.WriteLine($"{count} lines");Check your prediction
1: [Read]
2: []
3: [Review]
3 linesThe brackets show that the blank line is a real, empty string. The input also doesn't need a final newline; delete it from the box to check.
Your turn
Count the real lines
Read every line of input. Trim each one, and print the non-blank ones in the form Task: Read. At the end, print how many lines were blank, as Blank: 2. Use #nullable enable, and make sure no warnings appear. Check it with empty input too.
#nullable enable
string? line = Console.ReadLine();
Console.WriteLine(line);Hint 1: the idea
Use the read-until-null loop. Inside it, you've already proved the line isn't null, so no warning appears when you call its methods.
Hint 2: a first step
Copy the loop from the example above and print each trimmed line. Then add the test for a blank one.
Show one solution
#nullable enable
int blank = 0;
string? line;
while ((line = Console.ReadLine()) is not null)
{
string trimmed = line.Trim();
if (trimmed.Length == 0)
{
blank++;
continue;
}
Console.WriteLine($"Task: {trimmed}");
}
Console.WriteLine($"Blank: {blank}");Task: Read
Task: Review
Blank: 2ReadLine returns null only when the input has ended, so the loop condition is the only place that needs to know about the end. Trimming first means a line of spaces counts as blank too, and continue sends blank lines straight back to the next read.
Run the solution with an empty input box. Which lines still print, and why doesn't the program crash?
Completion is saved in this browser only. It does not measure mastery.