Beginner · Getting started with C# and .NET · topic 3 of 43 · 25 min
Your first program: output, input and the Main method
Explanation
Statements and semicolons
A statement is one instruction, like "print this". In C#, every statement ends with a semicolon (;). Blocks of statements are wrapped in curly braces { }. C# is case-sensitive: Console and console are different names, and only Console exists.
Console.WriteLine("Hello, World!");
Console.WriteLine("Welcome to C#");Top-level statements
Since C# 9, you can write statements directly in Program.cs. This is called top-level statements. The compiler quietly wraps them in a Main method for you. Only one file in a project may have top-level statements, because a program can have only one starting point.
The classic Main method
Older projects, many tutorials and many company codebases write the full form. Main is the entry point, the first method that runs. It must be static (it belongs to the class, not to an object, explained in module 4).
namespace Hello;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}| Part | Meaning |
|---|---|
| namespace Hello; | A name that groups related classes, like a surname groups a family. |
| class Program | A class is a container for code. Every method lives inside a class. |
| static void Main | static: no object needed. void: returns nothing. Main: the entry point. |
| string[] args | An array of strings from the command line. |
Comments
// A single-line comment: the compiler ignores it
/* A comment
over many lines */
Console.WriteLine("Comments are for humans"); // after code tooWrite and WriteLine
Console.Write("Rs ");
Console.Write(250);
Console.WriteLine();
Console.WriteLine("Line one\nLine two");
Console.WriteLine("Path: C:\\Users\\Asha");Rs 250
Line one
Line two
Path: C:\Users\AshaReading input: Console.ReadLine
Console.ReadLine() waits for the user to type a line and press Enter, then gives it back as a string. Its type is string? with a question mark, meaning it may be null (no value) if there is no more input, for example when input is redirected from an empty file. Module 3 explains null in depth.
Console.Write("Your name: ");
string? name = Console.ReadLine();
Console.WriteLine($"Namaste, {name}!");Typing Asha shows Namaste, Asha!. ReadLine always gives text. To get a number you must convert it, which the next topics cover.
using directives
Console lives in the System namespace. Its full name is System.Console. A using directive at the top of a file (using System;) lets you write the short name. New projects have ImplicitUsings turned on, which adds the common ones (System, System.IO, System.Linq, System.Collections.Generic and a few more) for you.
Common mistakes and fixes
| Mistake | Compiler message | Fix |
|---|---|---|
| Missing ; at the end | CS1002: ; expected | Add the semicolon |
| console.WriteLine | CS0103: The name 'console' does not exist in the current context | Use capital C: Console |
| Single quotes around text: 'Hi' | Too many characters in character literal | Use double quotes for strings |
| Two files with top-level statements | Only one compilation unit can have top-level statements | Keep them in Program.cs only |
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Your first program: output, input and the Main method · 1/7
In this lesson you will write your first C# program, print text and read what the user types.
Prefer a video by a teacher? Find free videos on “C# hello world Console.WriteLine ReadLine top level statements beginners” on YouTube ↗
In real life: A chai stall greeting screen
A small chai stall in Indore puts an old laptop on the counter. A tiny console app asks the customer's name and greets them, a first step towards a token system.
Console.WriteLine("=== Sharma Chai Corner ===");
Console.Write("Your name please: ");
string? name = Console.ReadLine();
Console.WriteLine($"Welcome, {name}! Your chai will be ready in 5 minutes.");Practice (0/3 done)
What does this print? Console.Write("A"); Console.Write("B"); Console.WriteLine("C");
Which line has the bug? 1: Console.WriteLine("Total:"); 2: console.WriteLine(250); 3: Console.WriteLine("Done");
Write a console app that asks for the user's name and city, then prints one line like: "Asha from Pune, welcome to C#!". Then write the same program again in the classic static void Main style.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Types and variablesGetting started with C# and .NET · same module
- The structure of a C programC Programming (from zero to systems) · Getting started with C
- How JavaScript runsJavaScript Programming (language in depth) · First steps: values, variables and operators
- Methods: write once, use many timesDecisions, loops and methods
- Your first program: main, cout and namespacesC++ Programming (modern C++ and DSA) · First steps: compiling, output, variables and input
- Functions, statements and expressionsRust Programming · Types, control flow and functions
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What ends a statement in C#?
Answer: A semicolon ;
Every C# statement ends with a semicolon.
What type does Console.ReadLine() return?
Answer: string? (a string that may be null)
ReadLine returns string?, because it returns null when there is no more input.
How many files in one project may contain top-level statements?
Answer: Exactly one
A program has one entry point, so only one file can hold top-level statements.
string city = "Pune"; What does Console.WriteLine($"I live in {city}"); print?
Answer: I live in Pune
The $ makes it an interpolated string, so {city} is replaced by its value.
In static void Main(string[] args), what does void mean?
Answer: The method returns nothing
void means the method does not return a value.
In int main(int argc, char *argv[]), what is argv[0]?Related
What does the engine do with // comments?Related
Which is a good method name in C#?Related
Answer: CalculateTotal
C# methods use PascalCase and usually start with a verb.
What does return 0; at the end of main mean?Related
Answer: The program finished successfully
The value returned from main is the exit code. 0 means success.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Getting started with C# and .NET” before its exam: Types and variables, Operators, conversions and parsing input.
- Module 1 exam: Getting started with C# and .NET · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Decisions, loops and methods. 43 topics left in the course before the final exam.