Loading...

C# First Program

You already installed the SDK and ran a first lab. The screen showed pine. This page stays with that same program shape and asks a different question: which line inside the file wrote that word, and which lines wrote nothing. You are not installing .NET again. The version line from the last page can stay as it was. Keep the public class named Pine, keep Main, and keep Console.WriteLine("pine");. The screen you are explaining is still one line, and that line is pine.

The file has more than one line, so a first guess is that each line of the file becomes a line on the screen. That guess is wrong, and the rest of this page shows why. Several lines name something. Only one of them writes. The braces group those lines. They do not write either. When you can point at the writing line with the file in front of you, this page is done. The next page adds a note the screen does not show. It does not change pine.

the whole Pine file

csharp
using System;

public class Pine
{
    public static void Main()
    {
        Console.WriteLine("pine");
    }
}

using System prepares short names

The first useful line is using System;. Read it as a preparation line, not as a sentence the screen will print. It brings names from the System namespace into short form so Console can appear without a long path. You do not see the word using on the screen. You do not see System on the screen. Delete this line in a wrong experiment and the short name Console may fail later — that is a compile story for another day. On this page the line stays, and the screen stays pine because this line does not write.

The class line names the type

The next type line is public class Pine. Read it as three words with a job, not as screen text. public means other code is allowed to use this type. You do not see public on the screen. class means this file is defining one named unit. You do not see class on the screen. Pine is the name of that unit. On this teaching shape the file stem matches that name. Change the name on this line and the file name habit has to change with it. That rule is about the type and the file. It is not about the screen. Look at the screen again. The screen does not show Pine. It shows pine. The capital P and the small p are not the same word. The class line never writes its name onto the screen.

The character after that line is an opening brace, {. The matching closing brace later in the file ends the class body. Everything between those braces belongs to class Pine. The braces are fences. They tell C# where this unit starts and where it ends. A fence is not text for the screen. You will not see { or } next to pine. If you delete the opening brace, the file is broken and the compiler writes an error instead of staying quiet. That error is not this page. This page keeps both braces, and the screen stays pine.

The Main line is where the runtime starts

The next line is public static void Main(). This is the line people point at when they are asked where the program starts, and they are right about the start. They are wrong if they then say the screen shows Main. The runtime looks for a method with this shape when you run the project. It starts running at this line. Starting is not the same thing as writing. The words on the line are instructions about how to start. They are not words for the screen.

Take the words one at a time, and check each one against the screen. public again means the method can be started from outside. static means the runtime can start this method without first building an object. You have not met objects yet, so the only fact you need is that static is required on this teaching shape and that it is not printed. void means this method does not hand a value back to a caller. There is no caller story on this page, and void is not on the screen. Main is the name the runtime looks up in this shape. The screen still does not show those four letters. Parentheses mark the parameter list. On this page the list is empty. Empty parentheses do not print either.

So the Main line can be true in two ways at once. The runtime did start there. The screen did not receive a single word from that line. If your eyes rest on Main because it sits in the middle of the file, move them. The word you remember from the screen is pine, and pine is not written on this line.

The inner line writes pine

Inside Main, between its own braces, there is one more line. That line is Console.WriteLine("pine");. This is the line that writes the screen. It is indented in the file so you can see that it sits inside Main. The indent is spaces. Spaces are not written on the screen either. The runtime runs this inner line because it is inside the method where the program starts. The screen uses what this inner line writes. It does not use the name of the outer method.

Read the inner line from the left. Console is the screen helper this course uses. WriteLine means write this, then end the line. The part in parentheses is what gets written. Inside the parentheses the word pine sits between double quotes. The quotes mark pine as text. They are not part of the text. Take them away in your head and the word that remains is pine, and that is the whole screen. Leave them in the file. Without the quotes, the compiler does not treat pine as that text literal. The semicolon at the end finishes the statement for C#. You do not see a semicolon on the screen. You see pine, and then the line ends because WriteLine ended it.

Here is that writing line on its own, so the other lines cannot hide it.

only this line writes the screen

csharp
Console.WriteLine("pine");

Nothing in the dotnet run command chooses the word pine. The command only starts the project. The word comes from the quotes. If the quotes held a different word, the screen would show that word instead, and this page does not do that. The quotes on this page hold pine, so the screen is pine. One line. No second line. No quotes on the screen. No word Main. No word Pine.

📊 Which line the screen shows

The line The screen
using System; nothing from this line
public class Pine nothing from this line
public static void Main() nothing from this line
Console.WriteLine("pine") pine

What people misread

The usual miss is to read the file from the top and assume the most familiar word is the output. Main looks important, and it is important, because the runtime will not start this shape without it. Importance is not output. You can cover the Main line with a finger and the writing line is still Console.WriteLine("pine");. You cannot cover the writing line and still explain the screen. Another miss is to say the screen shows Pine because the class is named Pine. The screen uses the word between the quotes, and that word is spelled with a small p. A third miss is to count lines. The file shows several lines and several braces. The screen shows one. Braces, the words public and static and void, the name Main, the name Pine, and using System; are all still in the file after the run. The run does not delete them. It also does not print them.

The screen comes from one line

You can put a finger on Console.WriteLine("pine"); and say that this is the line the screen comes from. You can put a finger on public static void Main() and say that the runtime starts there and that the word Main is not on the screen. You can put a finger on public class Pine and say that this name matches the teaching type and does not match the screen. The next page puts a note in the file. The note is for a person reading the file. The screen will still be pine, because the note is not a second WriteLine.
Keep leftover Java System.out.println("mint") habits off this page when you practice the C# writing line. Keep inventing ASP.NET routes off this page. Keep treating dotnet --version as the screen word off this page — the version line was install proof, not this program’s output. When you can name the writing line and refuse the false guesses from Main and Pine, First Program did its job and you are ready for C# Comments if you continue.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

❓
6
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