Introduction to C#
Reviewed & published by Brayan K
C# is a modern, object-oriented programming language developed by Microsoft that runs on the .NET platform and is widely used to build web apps, desktop software, games (with Unity), and cloud services.
Part of the free C# course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
By the end of this lesson you'll understand what C# and .NET are, you'll have written and run your own C# program, and you'll be printing clean, formatted output with Console.WriteLine and string interpolation — the very first skills every C# developer needs.
What You'll Learn
- Understand what C# is and why it's one of the most in-demand programming languages
- Learn how C# fits into the .NET ecosystem (desktop, web, games, mobile)
- Write and run your first C# program with Console.WriteLine()
- Understand the anatomy of a C# program: using, class, Main, and statements
- Use string interpolation to combine text and variables in output
- Know the difference between Console.Write() and Console.WriteLine()
💡 Real-World Analogy
Think of C# as a universal remote control. Just like one remote can control your TV, sound system, and streaming box, C# can build desktop apps, websites, mobile apps, and games — all with the same language. The .NET framework is the signal receiver that makes everything work together. You learn one "remote" and control an entire ecosystem.
What is C# and .NET?
C# (pronounced "C Sharp") is a modern, object-oriented programming language created by Microsoft in 2000. It was designed by Anders Hejlsberg (who also created TypeScript and Delphi) to be powerful yet approachable — combining the performance of C++ with the productivity of languages like Java and Python.
C# runs on the .NET platform, which provides a massive standard library, a garbage collector for automatic memory management, and cross-platform support. With .NET 8+, your C# code runs on Windows, macOS, and Linux. Here's what you can build:
- 🎮 Games — Unity (the world's most popular game engine) uses C# exclusively
- 🌐 Web APIs & Websites — ASP.NET Core powers enterprise backends at Microsoft, Stack Overflow, and more
- 🖥️ Desktop Apps — Windows Forms, WPF, and .NET MAUI for cross-platform UIs
- 📱 Mobile Apps — .NET MAUI and Xamarin for iOS/Android from a single codebase
- ☁️ Cloud & Microservices — Azure Functions, gRPC services, and containerised APIs
C# is widely used for business software, games (through Unity) and cloud services, so it turns up in job listings in all three areas.
📊 C# Program Anatomy
| Part | Code | What It Does |
|---|---|---|
| Using directive | using System; | Imports the System namespace so you can use Console |
| Class declaration | class Program {} | Every C# program lives inside a class |
| Main method | static void Main() | The entry point — where your program starts running |
| Statement | Console.WriteLine("..."); | An instruction that does something — ends with ; |
| String interpolation | $"Hello {name}" | Embed variables inside strings with $ prefix |
🖥️ Setting Up C#
You can practise C# right here in the browser, but to run C# on your own machine:
Option 1: Visual Studio (Recommended for Windows)
- Download Visual Studio Community (free) from visualstudio.microsoft.com
- Select the ".NET desktop development" workload during installation
- Create a new "Console App" project and start coding
Option 2: VS Code + .NET SDK (All platforms)
- Install the .NET SDK from dotnet.microsoft.com
- Install VS Code with the C# extension
- Run dotnet new console -n MyApp in terminal, then dotnet run
Option 3: Online (No install)
Use dotnetfiddle.net or the code editors in this course to practise instantly.
1. Your First C# Program
Every C# journey starts with "Hello, World!". Console.WriteLine() prints text to the console and then moves to a new line. The text you want to print goes inside "double quotes", and the whole statement ends with a semicolon ;. Read this worked example first and run it, then you'll write your own.
using System;
class Program
{
static void Main()
{
// Console.WriteLine prints a line of text, then moves to a NEW line.
Console.WriteLine("Hello, C# World!"); // prints: Hello, C# World!
Console.WriteLine("Welcome to C# programming!"); // prints on its own line
Console.WriteLine("Let's build something amazing!");
// ✅ Expected output (three separate lines):
// Hello, C# World!
// Welcome to C# programming!
// Let's build something amazing!
}
}Your turn. The program below is almost complete — fill in the two blanks marked ___ with your own text (remember the "double quotes"), then run it.
using System;
class Program
{
static void Main()
{
// 🎯 YOUR TURN — replace each ___ then press "Try it Yourself".
// 1) Print a greeting of your choice
Console.WriteLine(___); // 👉 put your text in "double quotes"
// 2) Print your name on its own line
Console.WriteLine(___); // 👉 e.g. "My name is Sam"
// ✅ Expected output (example):
// Hello there!
// My name is Sam
}
}2. Console Output & String Interpolation
Console.Write() prints without a new line, so the next output continues on the same line, while Console.WriteLine() finishes the line for you. String interpolation — the $"..." syntax — lets you drop variables straight into text inside {curly braces}, which reads far better than gluing strings together with +. Escape characters like \n (new line) and \t (tab) let you shape the layout.
using System;
class Program
{
static void Main()
{
// WriteLine adds a new line at the end; Write does NOT.
Console.WriteLine("=== Welcome to C# ===");
Console.Write("This stays on "); // no new line after this
Console.WriteLine("the same line."); // joins: This stays on the same line.
// $"..." is string interpolation: {variable} is swapped for its value.
string language = "C#";
int year = 2026;
Console.WriteLine($"Learning {language} in {year}!"); // Learning C# in 2026!
// Escape characters: \n = new line, \t = tab, \" = a literal quote.
Console.WriteLine("Line 1\nLine 2"); // Line 1 then Line 2 (two lines)
Console.WriteLine("Tab\there"); // Tab here (tab gap)
Console.WriteLine("She said \"hello\""); // She said "hello"
}
}
// ✅ Expected output:
// === Welcome to C# ===
// This stays on the same line.
// Learning C# in 2026!
// Line 1
// Line 2
// Tab here
// She said "hello"Now you try. Fill in the two blanks: one puts a variable inside interpolation braces, the other prints a number on the same line as some text.
using System;
class Program
{
static void Main()
{
// 🎯 YOUR TURN — fill in the blanks marked with ___
string course = "C#";
// 1) Use interpolation to print "I'm learning C# today!"
Console.WriteLine($"I'm learning {___} today!"); // 👉 put the variable name in the braces
// 2) Use Write (no new line) then WriteLine so both appear on ONE line
Console.Write("Score: ");
Console.WriteLine(___); // 👉 print the number 100 (no quotes — it's a number)
// ✅ Expected output:
// I'm learning C# today!
// Score: 100
}
}3. A Taste of Variables
Variables store data. C# is strongly typed, which means every variable has a specific type (string, int, double, bool) that fixes what it can hold. You'll cover types in depth in the very next lesson, but here's a preview so the next lesson feels familiar:
using System;
class Program
{
static void Main()
{
// A variable is a named box that stores ONE type of value.
string name = "Alice"; // text -> always in "double quotes"
int age = 25; // whole number (no decimal point)
double height = 1.68; // decimal number
bool isStudent = true; // true or false (lowercase in C#)
// Drop each variable into the text with {curly braces}.
Console.WriteLine($"Name: {name}"); // Name: Alice
Console.WriteLine($"Age: {age}"); // Age: 25
Console.WriteLine($"Height: {height}m"); // Height: 1.68m
Console.WriteLine($"Student: {isStudent}"); // Student: True
}
}
// ✅ Expected output:
// Name: Alice
// Age: 25
// Height: 1.68m
// Student: TruePro Tips
- 💡 C# is case-sensitive: Console works, console doesn't. Watch your capitalisation.
- 💡 Every statement ends with a semicolon (;) — forgetting it is the most common beginner error.
- 💡 Use string interpolation ($"...") instead of concatenation ("Hello " + name). It's cleaner and easier to read.
- 💡 C# 9+ supports top-level statements: you can skip the class and Main method for simple programs. But learn the full structure first — you'll need it for real projects.
Common Errors (and the fix)
- "CS1002: ; expected" — you forgot the semicolon at the end of a statement. Every statement must end with ;, e.g. Console.WriteLine("Hi");.
- "CS0103: The name 'Console' does not exist in the current context" — you're missing using System; at the top, so the compiler doesn't know what Console is. Add that line.
- "CS0234: The type or namespace name '…' does not exist in the namespace 'System'" — a using points at something that isn't there (a typo or a missing package). Check the namespace spelling.
- "CS0117: 'Console' does not contain a definition for 'Writeline'" — wrong capitalisation. It must be Console.WriteLine with a capital L; C# is case-sensitive.
- "CS1525 / CS1056: Unexpected/invalid token" — usually a mismatched brace. Every { needs a matching }; use an editor that highlights matching braces.
- "CS1012: Too many characters in character literal" — you used 'single quotes' around text. Strings use "double quotes"; single quotes are only for one char like 'A'.
📋 Quick Reference
| Task | Code | Result |
|---|---|---|
| Print a line | Console.WriteLine("Hi"); | Hi (then new line) |
| Print, no new line | Console.Write("Hi"); | Hi (cursor stays) |
| Interpolation | $"Hi {name}" | Hi Sam |
| New line inside text | "Line1\nLine2" | two lines |
| Tab inside text | "A\tB" | A B |
| Import for Console | using System; | enables Console |
Frequently Asked Questions
Q: What's the difference between Console.Write and Console.WriteLine?
WriteLine prints your text and then moves to a new line. Write prints your text but leaves the cursor right after it, so the next output continues on the same line.
Q: Why do I need using System; at the top?
Console lives in the System namespace. The using System; line tells the compiler where to find it. Leave it out and you get CS0103: The name 'Console' does not exist.
Q: Do I really have to write the class and Main every time?
Modern C# (9+) supports "top-level statements" that let you skip them for small programs. But the full structure is what real projects use, so it's worth learning first — that's why this course shows it.
Q: Why did my code break when I typed console.writeline?
C# is case-sensitive. It must be Console.WriteLine with the capital C, W, and L. Capitalisation is part of the name, not a style choice.
Mini-Challenge: "About Me" Banner
No blanks this time — just a brief and a blank canvas (with a comment outline to keep you on track). Write it yourself, run it, and check your output against the example in the comments. This is exactly the kind of small program you'll build constantly.
using System;
class Program
{
static void Main()
{
// 🎯 MINI-CHALLENGE: "About Me" banner
// 1. Print a top border line, e.g. "====================="
// 2. Print three facts about you on three lines using Console.WriteLine,
// and use string interpolation ($"...") for at least one of them.
// 3. Print a matching bottom border line.
//
// ✅ Example output:
// =====================
// Name: Sam
// Learning: C#
// Goal: build games with Unity
// =====================
// your code here
}
}🎉 Lesson Complete
- ✅ C# is a modern, versatile language for games, web, desktop, and mobile
- ✅ .NET is the platform that runs C# code on Windows, macOS, and Linux
- ✅ Every program needs using System;, a class, and a Main method
- ✅ Console.WriteLine() prints with a new line; Console.Write() prints without
- ✅ String interpolation: $"Hello {name}" embeds variables in strings
- ✅ C# is strongly typed — every variable has a declared type
- ✅ Statements end with ; and code blocks use {} braces
Practice quiz
Which method prints text and then moves to a new line?
- Console.Write()
- Console.WriteLine()
- Console.Print()
- Console.Log()
Answer: Console.WriteLine(). Console.WriteLine() prints the text and then moves the cursor to a new line; Console.Write() stays on the same line.
Which line must be at the top so you can use Console?
- import System;
- using System;
- include System;
- namespace System;
Answer: using System;. Console lives in the System namespace, so you need 'using System;' to access it.
Every C# statement must end with which character?
- A colon :
- A comma ,
- A semicolon ;
- A full stop .
Answer: A semicolon ;. Statements end with a semicolon; forgetting it gives the error 'CS1002: ; expected'.
What is the entry point method where a C# program starts running?
- Start()
- Run()
- Main()
- Begin()
Answer: Main(). Main is the entry point — execution begins there.
Which is the correct string interpolation syntax to embed a variable name?
- "Hello {name}"
- $"Hello {name}"
- f"Hello {name}"
- "Hello " {name}
Answer: $"Hello {name}". Interpolation needs the $ prefix, then variables go inside {curly braces}.
C# is case-sensitive. Which of these works?
- console.writeline()
- Console.Writeline()
- Console.WriteLine()
- CONSOLE.WRITELINE()
Answer: Console.WriteLine(). It must be Console.WriteLine with capital C, W, and L — capitalisation is part of the name.
Which symbols wrap text (a string) in C#?
- 'single quotes'
- "double quotes"
- `backticks`
- (parentheses)
Answer: "double quotes". Strings use double quotes; single quotes are only for a single char like 'A'.
What does Console.Write() do differently from Console.WriteLine()?
- It prints to a file instead
- It does not add a new line at the end
- It only prints numbers
- It clears the screen first
Answer: It does not add a new line at the end. Write() leaves the cursor on the same line, so the next output continues right after it.
What does it mean that C# is strongly typed?
- Code must be typed quickly
- Every variable has a declared type that fixes what it can hold
- All variables are text
- Types are optional everywhere
Answer: Every variable has a declared type that fixes what it can hold. Strongly typed means each variable has a specific type (string, int, double, bool, ...) fixing what it can store.
What platform does C# run on, giving it cross-platform support?
- .NET
- JVM
- Node.js
- Ruby on Rails
Answer: .NET. C# runs on the .NET platform, which works on Windows, macOS, and Linux.
Continue this course
- Next: Variables & Data Types — int, string, bool, double — declare and use the core C# types
- Quick reference: C# cheat sheet