C#: Introduction to C#
If you've ever played with building blocks, you already understand the essence of programming — combining simple pieces into complex structures. C# is exactly that kind of "building block": clean syntax, type-safe, and powerful, letting you build professional-grade applications from your very first line of code.
1. What Is C#
C# (pronounced "C Sharp") is an object-oriented programming language developed by Microsoft in 2000, led by chief designer Anders Hejlsberg, and officially released in 2002. The # in its name is an arrangement of four + signs, hinting that it's one step beyond C/C++.
C# runs on the .NET platform, compiled and executed by the CLR (Common Language Runtime), with rich API support from the FCL (Framework Class Library). Simply put:
| Component | Role | Analogy |
|---|---|---|
| C# Language | Syntax rules for writing code | A car's driving manual |
| CLR | Compiles and runs programs | A car's engine |
| FCL | Provides ready-made functionality | A car's parts warehouse |
| .NET SDK | Development toolkit | The entire car factory |
2. Why Learn C#
- High market demand: C# consistently ranks in the top 10 programming languages, especially dominant in enterprise and game development
- Elegant syntax: C# maintains strong type safety while continuously introducing concise syntactic sugar, making code highly readable
- Cross-platform: .NET 5+ unified the Windows, Linux, and macOS platforms
- Mature ecosystem: Visual Studio is widely regarded as one of the most powerful IDEs, and NuGet offers hundreds of thousands of third-party libraries
- Beginner-friendly: Strong typing plus comprehensive compiler hints help beginners make fewer mistakes and get up to speed quickly
- Broad career paths: From game development to enterprise applications, from web to mobile — one language covers many tracks
3. What C# Can Do
| Domain | Tech Stack | Description |
|---|---|---|
| Game Development | Unity + C# | Unity is the world's most popular game engine, using C# as its scripting language |
| Web Applications | ASP.NET Core | High-performance Web APIs and MVC websites |
| Enterprise Applications | .NET + EF Core | Large business systems like ERP and CRM |
| Mobile Applications | .NET MAUI / Xamarin | One codebase running on both Android and iOS |
| Desktop Applications | WPF / WinForms | Native Windows desktop programs |
| Cloud Services | Azure + C# | The preferred language for the Microsoft cloud ecosystem |
| IoT & Embedded | .NET nanoFramework | Running C# on microcontrollers |
| Machine Learning | ML.NET | Native .NET machine learning framework |
4. Key Features of C#
(1) Type Safety
C# is a strongly typed language. The compiler checks type matching before the program runs, preventing a large class of runtime errors.
▶ Example
int age = 25;
string name = "C#";
age = name;
This produces a compile-time error — you cannot assign a string to an int variable:
error CS0029: Cannot implicitly convert type 'string' to 'int'
(2) Object-Oriented
C# has been object-oriented since its inception, supporting the three core pillars of encapsulation, inheritance, and polymorphism. Everything is an object.
▶ Example
class Animal
{
public string Name { get; set; }
public virtual void Speak() => Console.WriteLine($"{Name} makes a sound");
}
class Dog : Animal
{
public override void Speak() => Console.WriteLine($"{Name} says: Woof!");
}
var dog = new Dog { Name = "Buddy" };
dog.Speak();
Buddy says: Woof!
(3) Cross-Platform
.NET 5+ unified the older .NET Framework (Windows only) and .NET Core (cross-platform). Now a single codebase can run on Windows, Linux, and macOS.
(4) Modern Syntax Evolution
C# releases a major version roughly every two years, continuously adopting modern language features like functional programming, pattern matching, and async programming to keep the language vibrant.
5. A Brief History of C#
| Version | Release Year | Key Features |
|---|---|---|
| C# 1.0 | 2002 | Basic OOP, classes, interfaces, delegates |
| C# 2.0 | 2005 | Generics, anonymous methods, nullable types |
| C# 3.0 | 2007 | LINQ, lambda expressions, extension methods, auto-properties |
| C# 4.0 | 2010 | Dynamic binding, named parameters, covariance/contravariance |
| C# 5.0 | 2012 | async/await asynchronous programming |
| C# 6.0 | 2015 | String interpolation, null-conditional operator, exception filters |
| C# 7.0 | 2017 | Pattern matching, tuples, local functions |
| C# 8.0 | 2019 | Nullable reference types, async streams, default interface methods |
| C# 9.0 | 2020 | Record types, top-level statements, init accessors |
| C# 10.0 | 2021 | Global using, file-scoped namespaces, constant string interpolation |
| C# 11.0 | 2022 | Raw string literals, list patterns, required modifier |
| C# 12.0 | 2023 | Primary constructors, collection expressions, inline arrays |
6. .NET Ecosystem Evolution
| Era | Name | Description |
|---|---|---|
| 2002—2019 | .NET Framework | Windows only, up to 4.8, no new features added |
| 2016—2019 | .NET Core | Cross-platform rewrite, 1.0 → 2.0 → 3.1 |
| 2020 | .NET 5 | Unified .NET Core and Mono, skipped 4 to avoid confusion |
| 2021 | .NET 6 | First LTS (Long-Term Support) unified release |
| 2022 | .NET 7 | STS (Standard Term Support) release |
| 2023 | .NET 8 | Second LTS release, recommended for production use |
| 2024 | .NET 9 | Latest STS release |
7. C# vs. Other Languages
| Feature | C# | Java | C++ | Python |
|---|---|---|---|---|
| Type System | Strongly typed, with type inference | Strongly typed, more verbose | Strongly typed, more complex | Dynamically typed |
| Execution | JIT compiled | JIT compiled | Native compiled | Interpreted |
| Performance | High | High | Highest | Lower |
| Syntax Evolution | Fast (new version every 2 years) | Slow (new version every 3–4 years) | Stable | Moderate |
| Cross-Platform | .NET 5+ | JVM | Requires recompilation | Native support |
| Game Development | Unity's first choice | LibGDX, etc. | Unreal's first choice | Scripting/auxiliary |
| Memory Management | Automatic GC | Automatic GC | Manual | Automatic GC |
| Learning Difficulty | Medium | Medium | High | Low |
8. Your First C# Program
▶ Example
Using top-level statements introduced in C# 9.0, you only need one line:
System.Console.WriteLine("Hello, World!");
Hello, World!
▶ Example
The traditional approach using the Main method entry point (the standard style before C# 9.0):
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Hello, World!
9. How to Master C#
- Practice hands-on: For every syntax point you learn, immediately write code to verify it — don't just read without practicing
- Understand the .NET runtime: Learn how the CLR compiles and executes code to help you write more efficient programs
- Leverage Visual Studio: IntelliSense, breakpoint debugging, and profiling tools make development far more productive
- Read the official docs: Microsoft Learn is the most authoritative reference
- Build step by step: Basic syntax → Object-oriented → LINQ → Async programming → Real-world projects
❓ FAQ
📖 Summary
- C# was designed by Microsoft's Anders Hejlsberg, released in 2002, and runs on the .NET platform
- C# has wide applications: games (Unity), web (ASP.NET Core), desktop (WPF), mobile (MAUI), cloud (Azure)
- Core features: type safety, object-oriented, cross-platform, and continuously evolving syntax
- The .NET ecosystem evolved from .NET Framework to .NET 8 LTS / .NET 9, achieving cross-platform unification
- C# evolves faster than Java, is safer than C++, and has stronger performance and stricter typing than Python
- New projects should use top-level statements + .NET 8 LTS
📝 Exercises
- Install the .NET SDK, run
dotnet new console -n MyFirstAppin the terminal, then use top-level statements to output your name and a brief self-introduction - Rewrite the top-level statements from exercise 1 in the traditional
Program.Mainstyle, and compare the two approaches - Research what LINQ (introduced in C# 3.0) is, and summarize its purpose in one sentence