Skip to content

Beginner · Getting started with C# and .NET · topic 3 of 43 · 25 min

Your first program: output, input and the Main method

Explanation

A new console app has one line: Console.WriteLine("Hello, World!");. This style, with code straight in the file, is called top-level statements. Older code puts the same line inside a static void Main method in a class. Console.WriteLine prints a line, and Console.ReadLine reads what the user types.

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.

csharp
Console.WriteLine("Hello, World!");
Console.WriteLine("Welcome to C#");
Two statements, each ending with a semicolon. The text in double quotes is a string.

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).

csharp
namespace Hello;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}
The same program in the classic style. args holds any words typed after the program name on the command line.
PartMeaning
namespace Hello;A name that groups related classes, like a surname groups a family.
class ProgramA class is a container for code. Every method lives inside a class.
static void Mainstatic: no object needed. void: returns nothing. Main: the entry point.
string[] argsAn array of strings from the command line.

Comments

csharp
// A single-line comment: the compiler ignores it
/* A comment
   over many lines */
Console.WriteLine("Comments are for humans"); // after code too
Comments explain WHY the code does something.

Write and WriteLine

csharp
Console.Write("Rs ");
Console.Write(250);
Console.WriteLine();
Console.WriteLine("Line one\nLine two");
Console.WriteLine("Path: C:\\Users\\Asha");
Write stays on the same line; WriteLine adds a new line at the end. \n is a new line inside a string and \\ is one backslash. These are escape sequences.
output
Rs 250
Line one
Line two
Path: C:\Users\Asha
The output.

Reading 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.

csharp
Console.Write("Your name: ");
string? name = Console.ReadLine();
Console.WriteLine($"Namaste, {name}!");
The $ before the quotes makes an interpolated string: values inside { } are filled in.

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

MistakeCompiler messageFix
Missing ; at the endCS1002: ; expectedAdd the semicolon
console.WriteLineCS0103: The name 'console' does not exist in the current contextUse capital C: Console
Single quotes around text: 'Hi'Too many characters in character literalUse double quotes for strings
Two files with top-level statementsOnly one compilation unit can have top-level statementsKeep them in Program.cs only
Tip: Read the error code (like CS1002) and the line number in the build output. Searching the code gives you the official explanation on Microsoft Learn.

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

Hello, C#

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.

csharp
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.");
Typing Ravi shows: Welcome, Ravi! Your chai will be ready in 5 minutes.

Practice (0/3 done)

Task 1 · Question

What does this print? Console.Write("A"); Console.Write("B"); Console.WriteLine("C");

Task 2 · Question

Which line has the bug? 1: Console.WriteLine("Total:"); 2: console.WriteLine(250); 3: Console.WriteLine("Done");

Task 3 · Practice

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

Question 1

1.What ends a statement in C#?

Question 2

2.What type does Console.ReadLine() return?

Question 3

3.How many files in one project may contain top-level statements?

Question 4

4.string city = "Pune"; What does Console.WriteLine($"I live in {city}"); print?

Question 5

5.In static void Main(string[] args), what does void mean?

Read more (free)

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

Answer: The program name

User arguments start at argv[1].

From The structure of a C program

What does the engine do with // comments?Related

Answer: Ignores them

Comments are notes for people; the engine skips them.

From How JavaScript runs

Which is a good method name in C#?Related

Answer: CalculateTotal

C# methods use PascalCase and usually start with a verb.

From Methods: write once, use many times

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.

From Your first program: main, cout and namespaces

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

  1. 2 more topics in “Getting started with C# and .NET” before its exam: Types and variables, Operators, conversions and parsing input.
  2. Module 1 exam: Getting started with C# and .NET · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Decisions, loops and methods. 43 topics left in the course before the final exam.

← Previous: Installing .NET and using the dotnet CLI