Your first C# program
What C# is for, the entry point every program needs, and Console.WriteLine as the way to make a program say something.
What C# is for
C# is a language from Microsoft, made for the .NET platform. It compiles: the text you write is turned into a program before anything runs, so most mistakes are reported before the program starts rather than during a run. Desktop tools, web services, games built with Unity and a great deal of software on Windows are written in it.
Here the file is compiled and run on a server, and you read what it printed. That gives this course one difference from an installed setup: nothing is read from the keyboard and no file is opened. Every example carries the values it needs inside the file, and everything it produces arrives as text under the editor.
C# and .NET are two things, and the names are often used as if they were one. C# is the language you type; .NET is the platform that supplies the runtime, the base library and the tools. Other languages can target the same platform, and a C# program can run wherever that platform has been installed. Keeping the two apart makes the error messages easier to read, because one comes from the compiler and the other from the runtime.
The shape that never changes
A C# file is a set of declarations. The one the compiler looks for first is the entry point: a class holding a method named Main. When the program starts, that method runs from its first line to its last, and the program ends. The class is the box, and the method is the door in it.
The first line, using System;, names the library that holds Console and the other everyday tools. Without it you would have to write the full path System.Console every time, so almost every file starts with it.
Saying something: Console.WriteLine
Console is the console of a program, and WriteLine writes one line into it and then moves to the next. Whatever sits inside the parentheses is printed: text in double quotes, a number, or a whole expression that is worked out first.
Because an expression is computed before it is printed, Console.WriteLine(2 + 3) sends the character 5 rather than the sum written out. That single fact is the idea behind this first chapter: you write an expression, and the program answers. Three pieces of punctuation carry the rest — braces open and close a block, and a semicolon ends a statement. C# insists on the semicolon, so it is worth making the habit of typing one at the end of every line of work.
The smallest complete program
using System;
class Program
{
static void Main()
{
// A comment is for the reader; the compiler skips it.
Console.WriteLine("Hello, C#!");
Console.WriteLine(2 + 3);
}
}Run it, then make it yours
Press Run and read the six lines. Then change one of the numbers, add a line with a sum of your own, and run it again to watch the output follow your edit.
The first run of a C# example compiles the file before it starts, so it takes a few seconds longer than the ones after it. Run is disabled while the compiler gets ready.
From source to a running program
The compiler does not turn your file straight into machine code. It reads the whole file first and turns it into an intermediate language, a compact set of instructions that the .NET runtime understands. The runtime then compiles those instructions for the machine it is on, just before they run.
That two-step arrangement is why a C# program can be checked so thoroughly before it starts: names, types and braces are all resolved while the first step runs. It is also why the output pane waits a moment before it fills. Nothing about the program has run yet; the compiler is deciding whether it is allowed to.
The shape of Main is not arbitrary
static means the method belongs to the class rather than to an object, so the runtime can call it before it has created anything. void means it hands nothing back to the caller. The empty parentheses mean it expects no arguments, which is one of the shapes the runtime accepts.
The name and the casing matter: Main with a capital M. A method called main is a different name, and the compiler will say the program has no entry point. That error is worth recognising, because it points at the shape of the whole file rather than at a single statement.
Three mistakes account for most first C# programs that refuse to compile: a missing using System;, so Console cannot be found; a missing semicolon at the end of a statement; and a brace that closes the wrong block. Read the first error first, because the ones after it are often consequences.
Try these next
- Print three lines: your name, a number, and the number doubled, each with its own
Console.WriteLine. - Replace one
WriteLinewithWriteand see which output runs together on one line. - Add a comment above one statement, then delete a semicolon and read the compiler message before putting it back.
A printed order slip
Run it and read the slip. Then change the quantity and the unit price, and add a second item and its quantity under the first, keeping the labels in the same order.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does the compiler look for before it can run a C# program?
