Loading...

C# Input

Type leaf after the program has started, and the screen shows leaf. That word is not in the file. The Pine file creates a name, plant, and waits. You type leaf and press Enter. Console.WriteLine(plant) writes leaf. Until now a different screen meant editing the storing line, because the word sat in the file between quotes. Keep the public class named Pine. You are not installing the SDK again.

The file waits for one typed line

Open the Pine file. The class name stays Pine. This page does not rename the type. Inside Main, replace the old storing line that held quotes. The new storing line is string plant = Console.ReadLine();. The keyword string still says this name holds letters. plant still starts with a small p. The = sign still stores letters in that name. What is new is the right side. Console.ReadLine() does not have quotes. There is no leaf on this line. The line asks the console for one typed line. The program waits there until you press Enter. When you press Enter, ReadLine stores the letters you typed before that Enter. It does not store the Enter itself. Then the line ends at the semicolon. Storing is still not writing. The program has not written its own line yet.

The writing line stays the bare name, the same shape as the file you kept on the last page: Console.WriteLine(plant);. No quotes. C# reads the name plant, writes the letters that name is holding, and ends the line. Those letters are whatever ReadLine just stored. If you typed leaf, the program writes leaf. If you typed stem, the program writes stem. The file did not change between those two runs. Save the file.

The file you keep

Keep this teaching shape. The word you will type is not in the file. Search the file for the letters l, e, a, f. They are not there. The stored line is named plant. The writing line uses plant and does not contain quotes.

the file you keep

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = Console.ReadLine();
        Console.WriteLine(plant);
    }
}

Read the file from the top and decide what can reach the screen. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. None of those words are letters for the screen. string plant = Console.ReadLine(); stores one typed line in plant. It writes nothing by itself. Console.WriteLine(plant); is the only writing line. It writes the stored letters and ends the line. The name plant is not written. Console is not written. The semicolon is not written.

What you type and what the program writes

Run it with dotnet run. After that command, the program is waiting on Console.ReadLine(). It has not finished, and it has not written leaf, because you have not given it a line yet. The cursor sits on a fresh line. Type the letters l, e, a, f. The terminal shows those letters as you press the keys. That showing is the terminal repeating your keys. It is not Console.WriteLine. Then press Enter.

Enter ends the wait. ReadLine stores leaf in plant and does not store the Enter. WriteLine then writes leaf and ends the line. You can see leaf, and under it leaf again. The first leaf is the keys the terminal showed. The second leaf is the program. The program's line has no quotes. It is four letters, then the line ends. The program finishes after that writing line. If the only leaf you meant to judge is the program's line, judge the line that appears after you press Enter.

If the program's line is plant, the writing line has quotes around plant. The file you keep does not. If dotnet run writes leaf at once and never waits, the file still stores a quoted word, such as string plant = "leaf";, and it never calls ReadLine. Look for quotes around leaf. The file you keep has none. You do not reinstall the .NET SDK for a screen that shows the wrong word.

Capitals are copied as you type them. If you type Leaf with a capital L and press Enter, the program's line is Leaf. The file still does not contain those letters. An empty Enter is also a real line. Press Enter without typing letters. ReadLine stores no letters in plant. WriteLine writes a line with no letters. The program ran. The wait ended because Enter arrived. The file you keep is the one you try with leaf, four small letters.

A space stays in the line

ReadLine reads until Enter, not until a space. Type leaf, then a space, then stem, then Enter. The letters stored in plant are leaf, a space, and stem. Console.WriteLine(plant) writes leaf stem. The space is a stored character, the same way a space inside quotes was stored when the word lived in the file. This page does not cut the line at the space. The file you keep is still the one-line reader. You only change what you type, not the file, when you want the space on the screen.

Two typed lines, two Enters

One program can take a second line. Under the first writing line, store a new line in the same name, then write the name again. The new storing line is plant = Console.ReadLine();. It does not start with string. plant already exists, so this line only replaces the letters the name holds. The next writing line is again Console.WriteLine(plant);. The program now waits twice. Each ReadLine waits for its own Enter.

This is the second file. It is not the file you keep. The file you keep waits once and writes once. Use this second shape when you want two typed lines, then put the single ReadLine and the single WriteLine back.

two typed lines

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = Console.ReadLine();
        Console.WriteLine(plant);
        plant = Console.ReadLine();
        Console.WriteLine(plant);
    }
}

Run dotnet run. The program waits. Type leaf and press Enter. The terminal shows leaf, and the program writes leaf under it. The program waits again. It has not finished. Type stem and press Enter. The terminal shows stem, and the program writes stem under it. The program's lines are leaf, then stem. The echoed keys sit above each of those lines. If both of the program's lines say leaf, the second ReadLine never ran, or you typed leaf both times. If the program writes stem before you have typed a second line, the second WriteLine is not sitting under a second ReadLine. The screen follows the order of the lines in the file, and each wait follows the ReadLine that has not received Enter yet.

Do not write string a second time for the same name. string plant = Console.ReadLine();, placed under a line that already created plant, is a different mistake. That line tries to create plant again. The build writes an error and does not stay quiet. The repair is to delete the second string and leave plant = Console.ReadLine();. The name is created once. Later lines only change the letters it holds. On the file you keep there is no second ReadLine at all.

Quotes around the name write the name

The writing line in the file you keep is Console.WriteLine(plant); with no quotes. If you put quotes around the name after a real typed line, the line is Console.WriteLine("plant");. Those quotes mark letters for the screen. The letters between them are p, l, a, n, t. C# does not look up the name. You can type leaf and press Enter, and ReadLine can store leaf in plant, and the program's line still shows plant. The typed word is not written, because the writing line never used the name.

This is the third file. It is not the file you keep. Use it long enough to see the single program line plant after you type leaf, then take the quotes off the writing line again.

the name written in quotes after a read

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = Console.ReadLine();
        Console.WriteLine("plant");
    }
}

The name has to exist after ReadLine

C# reads the file from the top. The line that fills plant has to be above the line that writes plant. If you put Console.WriteLine(plant); first, and only then write string plant = Console.ReadLine();, the writing line names plant before plant exists. The build writes an error. It does not stay quiet. There is no screen to trust while the build is writing an error. The repair is to move the ReadLine line back above the writing line.

This fourth file is the broken order. It is not the file you keep. Keep it only until the build writes the error, then put the ReadLine line back on top.

the name used before ReadLine

csharp
using System;

public class Pine
{
    public static void Main()
    {
        Console.WriteLine(plant);
        string plant = Console.ReadLine();
    }
}

WriteLine does not store a typed line

The storing line must call ReadLine, not WriteLine. If the line is string plant = Console.WriteLine();, the build writes an error. WriteLine writes to the screen and does not hand letters back into a name. It is not a waiting store. There is no typed leaf to trust while the build is writing an error. The repair is to put Console.ReadLine() back on the right side of =.

This fifth file is the wrong store call. It is not the file you keep. Keep it only until the build writes the error, then put ReadLine back.

WriteLine used where ReadLine belongs

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = Console.WriteLine();
        Console.WriteLine(plant);
    }
}

The screen is the line you typed

You can now fill a name from the keyboard and write that line without putting the word between quotes in the file. string plant = Console.ReadLine(); waits for Enter and stores the typed letters. Console.WriteLine(plant); writes those letters and ends the line. A second ReadLine waits again. Quotes around the name write the letters of the name instead. The ReadLine line has to sit above the first line that uses the name. WriteLine on the storing side is not a store. The next page starts value types, so a name can hold a number with a type other than string, while this page stays on one typed line of letters.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
7
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top