Your First C# Program
Every C# program needs a class with a static void Main() method — that's where execution starts. using System; at the top imports the System namespace, giving you access to Console without needing to write its full name (System.Console) every time.
Compiling happens first
When you hit run, the compiler checks your entire file and compiles it before a single line executes. If there's a type error or a syntax mistake anywhere, nothing runs at all — you get a compiler error instead of output.
Comments
Comments work like in most C-family languages: anything after // on a line is ignored completely, existing purely so humans reading the code later understand why something is written the way it is.
Run it yourself
Here's a small program with a comment and two Console.WriteLine calls. This was actually executed to get the output shown below — try changing the text and running it yourself using the button under the example.
using System;
class Program {
static void Main() {
// This is a comment -- ignored by the compiler
Console.WriteLine("Hello, world!");
Console.WriteLine("Learning C# one step at a time.");
}
}Hello, world! Learning C# one step at a time.
Next: variables and types — the primitive types, var inference, and what a real type-mismatch compile error looks like.
Practice exercise
Add a comment above your code, then print "Learning C# one step at a time."