C# Variables
Console.WriteLine(plant) writes leaf, and the writing line has no quotes. The word leaf sits on the storing line, under the name plant. Until now the word sat between quotes on the writing line itself, so a different screen meant editing that line. The quotes move to the line that stores the word. The screen still shows leaf. Keep the public class named Pine. You are not installing the SDK again.
A name holds the letters
Open the Pine file. The class name stays Pine. This page does not rename the type. Inside Main, above the writing line, add a line that creates a name and stores a word in it: string plant = "leaf";. The keyword string says this name holds letters. plant is the name, and it starts with a small p. The = sign stores the word. The quotes hold leaf, all small letters, the same word the last page put on the writing line. The semicolon ends this line. Then change the writing line so it no longer has quotes. It becomes Console.WriteLine(plant);. The name plant is bare. There is no quote before the p and no quote after the t. Save the file.
The file you keep
Keep this teaching shape. The name plant holds leaf. The writing line uses that name and does not contain quotes.
using System;
public class Pine
{
public static void Main()
{
string plant = "leaf";
Console.WriteLine(plant);
}
}
Read it from the top and decide what the screen can receive. using System; prepares short names and does not write the screen. public class Pine names the type. It does not write the screen. public static void Main() is where the runtime starts. None of those words are letters for the screen. The next line is the one that creates the name. string plant = "leaf"; stores the letters l, e, a, f in the name plant. Those letters are not written to the screen by this line. Storing is not writing. The screen stays blank through this line. The following line, Console.WriteLine(plant);, is the writing line. C# reads the name plant, takes the letters that name is holding, writes them, and ends the line. The screen shows leaf. One line. No quotes. The letters of the name, plant, are not on the screen. Console is not on the screen. The semicolon is not on the screen.
Run it with dotnet run. The screen shows leaf. If the screen shows plant, the writing line has quotes around plant. The file you keep does not. If the screen still shows a quoted word from the last page, the writing line is still using quotes instead of the bare name. Look at the letters between the quotes on the string line, and look at whether the writing line uses the bare name. You do not reinstall the .NET SDK for a screen that shows the wrong word.
Store line and write line
Keep the two jobs easy to tell apart.
📊 Variable habits for plant
| Line | Job | Screen |
|---|---|---|
string plant = "leaf"; |
stores letters in the name | nothing from this line |
Console.WriteLine(plant); |
writes the letters the name holds | leaf |
Console.WriteLine("plant"); |
writes the quoted letters | plant |
plant = "stem"; |
replaces the stored letters | nothing from this line alone |
Change the word, keep the writing line
The point of the name is that the writing line can stay put while the stored word changes. Under the first writing line, store a new word in the same name, then write the name again. The new storing line is plant = "stem";. It does not start with string. plant already exists, so this line only replaces the letters the name holds. leaf is replaced by stem, all small letters. The next writing line is again Console.WriteLine(plant);. It is the same shape as the first writing line. The screen now has two lines, because each WriteLine ends its line. The first line is leaf, from the letters the name held at that moment. The second line is stem, from the letters the name holds after plant = "stem";.
This is the second file. It is not the file you keep. The file you keep has one storing line and one writing line, and its screen is leaf. Use this second shape when you want to see the word change, 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 = "stem";
Console.WriteLine(plant);
}
}
Run dotnet run. The screen shows leaf, and the next line of the screen is stem. Two lines. leaf is on top because the first Console.WriteLine(plant); runs while plant still holds leaf. stem is under it because the second Console.WriteLine(plant); runs after plant = "stem";. The name plant is on neither screen line. If both screen lines say leaf, the second storing line is missing or still says "leaf". If both say stem, the first storing line already says "stem", or the first writing line sits below the change. The screen follows the order of the lines in the file.
Do not write string a second time for the same name. string plant = "stem";, 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 = "stem";. The name is created once. Later lines only change the letters it holds. On the file you keep there is no second storing line 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, the line is Console.WriteLine("plant");. Those quotes do what quotes did on the last page. They mark letters for the screen. The letters between them are p, l, a, n, t. C# does not look up the name, because a quoted word is not a name. The name plant can still hold leaf on the line above, and the screen still shows plant. The stored word leaf 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 line plant, then take the quotes off the writing line again.
using System;
public class Pine
{
public static void Main()
{
string plant = "leaf";
Console.WriteLine("plant");
}
}
Run dotnet run. The screen shows plant. One line. No quotes. Not leaf. Not stem. If you wanted leaf, the writing line has to be the bare name Console.WriteLine(plant);. The quotes belong on the storing line, around leaf, not on the writing line around the name. Keep leftover Java String habits mapped to the C# keyword string on this page. Keep inventing ASP.NET routes off this page.
The name has to exist first
C# reads the file from the top. The line that creates the name has to be above the line that uses the name. If you put Console.WriteLine(plant); first, and only then write string plant = "leaf";, 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 string 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 string line back on top.
using System;
public class Pine
{
public static void Main()
{
Console.WriteLine(plant);
string plant = "leaf";
}
}
The screen is the word the name holds
You can now store a word in a name and write that word without putting the word on the writing line. string plant = "leaf"; stores leaf. Console.WriteLine(plant); writes leaf and ends the line. A later plant = "stem"; changes the stored word, and a later WriteLine of the same name writes stem. Quotes around the name write the letters of the name instead. The creating line has to sit above the first line that uses the name. The next page reads a word that you type while the program is running, and stores that word in a name, so the screen can show letters that were not written between quotes in the file.
🧠 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