Introduction to C#
Lesson 1 โข Beginner Track
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 consistently ranked in the top 5 most in-demand programming languages, with average salaries ranging from ยฃ35,000โยฃ75,000 (UK) and $60,000โ$130,000 (US) depending on specialisation.
๐ 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 MyAppin terminal, thendotnet 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 moves to a new line. Try modifying the messages below:
Your First C# Program
Print messages to the console with Console.WriteLine().
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, C# World!");
Console.WriteLine("Welcome to C# programming!");
Console.WriteLine("Let's build something amazing!");
}
}2. Console Output & String Interpolation
Console.Write() prints without a newline (the next output continues on the same line). String interpolation with $"" lets you embed variables directly inside strings โ much cleaner than concatenation.
Console Output & Interpolation
Explore Write vs WriteLine and string interpolation.
using System;
class Program
{
static void Main()
{
// Writing output
Console.WriteLine("=== Welcome to C# ===");
Console.Write("This stays on ");
Console.WriteLine("the same line.");
// String interpolation
string language = "C#";
int year = 2024;
Console.WriteLine($"Learning {language} in {year}!");
// Escape characters
Console.WriteLine("Line 1\nLine 2");
Console.WriteLine("Tab\there");
C
...3. A Taste of Variables
Variables store data. C# is strongly typed โ every variable has a specific type (string, int, double, bool). We'll cover types in depth in the next lesson, but here's a preview:
Variables Preview
See how different data types work in C#.
using System;
class Program
{
static void Main()
{
// Variables hold data
string name = "Alice";
int age = 25;
double height = 1.68;
bool isStudent = true;
Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Height: {height}m");
Console.WriteLine($"Student: {isStudent}");
}
}Pro Tips
- ๐ก C# is case-sensitive:
Consoleworks,consoledoesn'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 Mistakes
- Missing semicolons โ Every statement must end with
;. The compiler will give you a "CS1002: ; expected" error. - Wrong capitalisation โ
Console.Writelinewon't work. It must beConsole.WriteLinewith a capital L. - Mismatched braces โ Every
{needs a matching}. Use an editor that highlights matching braces. - Forgetting "using System;" โ Without this, the compiler doesn't know what
Consoleis. You'll get a "CS0103: The name 'Console' does not exist" error. - Using single quotes for strings โ In C#, strings use double quotes
"hello". Single quotes'h'are for individual characters (char type).
๐ 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;, aclass, and aMainmethod - โ
Console.WriteLine()prints with a newline;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 - โ Next lesson: Variables & Data Types โ the building blocks of every program
Sign up for free to track which lessons you've completed and get learning reminders.