C# Strings
string plant = "leaf"; stores the four letters leaf, and Console.WriteLine(plant) writes leaf. The quotes stay in the file. They are not on the screen. The previous page stored the whole number 4 with no quotes around it. Here the pair of quotes is what holds the letters, and a missing pair makes the build write an error. Keep the public class named Pine. You are not installing the SDK again.
The quotes hold the letters
Open the Pine file. The class name stays Pine. This page does not rename the type. The last page kept int count = 4; with no quotes and no keyboard. This page does not keep that number. Delete the int line if it is still there. Delete any ReadLine line. The file you keep has no wait and no whole-number line. The letters are written only by the writing line that uses the name.
Inside Main, the storing line is string plant = "leaf";. string is the kind word. It says this name holds letters. plant is the name, and it starts with a small p. The = sign stores the letters. The pair is next. The first quote opens the letters. The four letters are l, e, a, f, in that order, all small. The second quote closes the letters. Nothing else is between those two quotes: no space, no extra letter, no second word. The semicolon ends the line. This line does not write the screen. Storing is not writing. The screen stays blank through this line. The quotes are part of the storing line. They are not part of the value the name holds after the line has stored it.
The writing line is the bare name: Console.WriteLine(plant);. No quotes. C# reads the name plant, writes the letters that name is holding, and ends the line. For the file you keep, those letters are l, e, a, f. The quote marks are not on the screen. The letters of the name, p, l, a, n, t, are not on the screen. string is not on the screen. Console is not on the screen. The semicolon is not on the screen. Save the file.
The file you keep
Keep this teaching shape. One kind word, string. One name, plant. One pair of quotes. Four letters between them, leaf. One writing line, and that line uses the bare name.
using System;
public class Pine
{
public static void Main()
{
string plant = "leaf";
Console.WriteLine(plant);
}
}
Read it from the top. 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 the letters on the screen. The storing line is string plant = "leaf";. That line stores the letters. It writes nothing. Console.WriteLine(plant); is the only writing line. It writes leaf and ends the line. One line. No quote marks on that screen line.
Run it with dotnet run. The program writes leaf. It does not wait for you to type. The letters were already in the file. If dotnet run waits, the file still has ReadLine in it. Delete that line and keep string plant = "leaf";. The screen shows leaf, one line, four letters, no quote marks. If the screen shows plant, the writing line has quotes around the name: Console.WriteLine("plant");. Those quotes mark the letters p, l, a, n, t. C# does not look up the name, so the stored leaf is not written. The file you keep has the bare name on the writing line, and the quotes around leaf on the storing line. If the screen shows 4, the file still has the whole number from the last page. The file you keep has leaf, not 4. You do not reinstall the .NET SDK for a screen that shows the wrong line.
A capital letter between the quotes is stored as that capital. string plant = "Leaf"; would make the writing line write Leaf, with a large L. The file you keep does not use that. It uses four small letters. Judge the screen against the letters that actually sit between the quotes, not against a word you only meant to write.
A space is one of the letters
The writing line can stay put while the letters between the quotes change. Under the first writing line, store a new set of letters in the same name, then write the name again. The new storing line is plant = "leaf stem";. It does not start with string. plant already exists, so this line only replaces the letters the name holds. Between the quotes this time there are nine characters: l, e, a, f, a space, s, t, e, m. The space is one of them. It is not a break into two storing lines, and it is not deleted when the screen is written. The next writing line is again Console.WriteLine(plant);. The screen now has two lines. The first is leaf, from the letters the name held at that moment. The second is leaf stem, from the letters the name holds after plant = "leaf stem";. One space sits between leaf and stem on that second line. The two words are not on two separate lines.
This is the second file. It is not the file you keep. The file you keep has one storing line, the letters leaf, and no space. Use this second shape when you want to see the space, then put the single storing line and the single writing line back.
using System;
public class Pine
{
public static void Main()
{
string plant = "leaf";
Console.WriteLine(plant);
plant = "leaf stem";
Console.WriteLine(plant);
}
}
Run dotnet run. The screen shows leaf, and the next line is leaf stem. Two lines. No quote marks. The name plant is on neither line. If the second line is leafstem, with no gap, the space is missing between the quotes. If both lines are leaf, the second storing line is missing or still says "leaf". The screen follows the order of the lines in the file, and it follows the letters between the quotes, space included. Do not write string a second time for the same name. string plant = "leaf stem";, placed under a line that already created plant, makes the build write an error. Delete the second string and leave plant = "leaf stem";.
Nothing between the quotes
A pair can be empty. string plant = ""; has the opening quote and the closing quote, and no letter between them. The kind word is still string. The name is still plant. The = sign still stores. What it stores is no letters. This is allowed. The build does not write an error for an empty pair. Console.WriteLine(plant); still runs. It writes the letters the name holds, and there are none, so the screen line has no letters. The line still ends. The program still finishes. You can see a blank line where leaf would have been. That blank line is the writing. It is not a wait, and it is not a build failure.
This is the third file. It is not the file you keep. The file you keep has leaf between the quotes. Keep this third file only until you have seen the blank line, then put "leaf" back between the quotes.
using System;
public class Pine
{
public static void Main()
{
string plant = "";
Console.WriteLine(plant);
}
}
Run dotnet run. The empty pair is allowed, so the build writes no error. The program writes a line with no letters and finishes. It does not wait. If you still see leaf, the quotes still have leaf between them. After you have seen the blank line, make the storing line string plant = "leaf"; again.
Without the quotes, leaf is a name
The pair is doing the work. Take both quotes away and the line is string plant = leaf;. There is no pair, so leaf is not letters. C# reads leaf as a name, and this file has no name leaf. 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 put the pair back, so the line is string plant = "leaf"; again. One quote is not a pair either. string plant = "leaf; opens the letters and never closes them before the semicolon. The build writes an error for that line too. Put the closing quote back before the semicolon.
This fourth file is the missing pair. It is not the file you keep. Keep it only until the build writes the error, then put the quotes back around leaf.
using System;
public class Pine
{
public static void Main()
{
string plant = leaf;
Console.WriteLine(plant);
}
}
Two quoted pieces joined with plus
A storing line can join two quoted pieces with +. The line string plant = "leaf" + "stem"; has two pairs. The first pair holds leaf. The second pair holds stem. The + joins those letters into one value: leafstem, with no space, because neither pair had a space. The writing line is still Console.WriteLine(plant);. The screen shows leafstem on one line. This is not two writing lines. It is one store that builds one set of letters from two quoted pieces.
This is the fifth file. It is not the file you keep. The file you keep has one pair and the letters leaf. Use this fifth shape when you want to see the join, then put the single pair back.
using System;
public class Pine
{
public static void Main()
{
string plant = "leaf" + "stem";
Console.WriteLine(plant);
}
}
The screen is the letters between the quotes
You can now store letters in a string name and write those letters without putting quotes on the writing line. string plant = "leaf"; stores leaf. Console.WriteLine(plant); writes leaf and ends the line. A later plant = "leaf stem"; stores the space as one of the letters. Empty quotes store no letters and write a blank line. Without the quotes, leaf is read as a name and the build writes an error. "leaf" + "stem" joins into leafstem. The next page stays with letters and looks at methods that work on a string name, such as asking how many letters it holds.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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