<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Getting started
All lessons in this course

Lesson 2 of 60 10 minBeginner

Your first C# program

Install the tools, create a console app, and understand every line of “Hello, World!”.

Install the tools

  1. Install Visual Studio Community (free) and tick the “.NET desktop development” workload. It includes the .NET SDK. Alternatively install the .NET SDK plus VS Code with the C# Dev Kit extension.
  2. Open a terminal and run dotnet --version. If you see a number like 9.0.100, the SDK is installed.

Create and run a console app

A console app runs in a text window. It is the best way to learn because there is no UI to distract you. In a terminal:

Terminal
dotnet new console -n HelloAppcd HelloAppdotnet run
Output
Hello, World!

In Visual Studio: Create a new project → Console App → Next, give it a name, then press F5 (run with debugger) or Ctrl+F5 (run without debugger).

Every line explained

Here is the classic, complete form of the program. Click each part to see what it does:

Interactive

Anatomy of a C# program

Click a line of code (or a button below) to see what that part means.

Program.cs
using System; namespace HelloApp{    class Program    {        static void Main(string[] args)        {            // Print a message to the console            Console.WriteLine("Hello, World!");        }    }}
static void Main
Main is the entry point: the first method that runs when your program starts. static means it runs without creating an object, void means it returns nothing, and string[] args receives any command-line arguments.

The short version: top-level statements

New projects (.NET 6+) generate a much shorter Program.cs. The compiler adds the namespace, class and Main method for you behind the scenes. Both versions do exactly the same thing.

Program.cs
// Program.cs (top-level statements)Console.WriteLine("Hello, World!");
Output
Hello, World!

Write vs WriteLine

Program.cs
Console.Write("Hello, ");Console.Write("Ravi");Console.WriteLine("!");      // ends the lineConsole.WriteLine("Next line");
Output
Hello, Ravi!
Next line

Rules C# is strict about

  • C# is case-sensitive: Console works, console does not.
  • Every statement ends with a semicolon ;.
  • Text (strings) goes in double quotes: "Hello".
  • Every { needs a matching }.

Comments

Comments.cs
// A single-line comment /* A comment that   spans several lines */ /// <summary>XML documentation comment, shown in IntelliSense.</summary>
Quick check

Which method runs first when a classic C# program starts?

Quick check

What is wrong with console.WriteLine("Hi")?

Practice

Write a program that prints your name on the first line, your city on the second line, and “I am learning C#” on the third line.

Show the solution
Solution.cs
Console.WriteLine("Raviselva");Console.WriteLine("Salem");Console.WriteLine("I am learning C#");
Output
Raviselva
Salem
I am learning C#

Remember

  • Create and run a console app with dotnet new console and dotnet run (or F5 in Visual Studio).
  • Main is the entry point; top-level statements are a shorter way to write it.
  • Console.WriteLine prints and starts a new line; Console.Write does not.
  • C# is case-sensitive, statements end with ;, blocks use { }.
0 of 60 lessons completed