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#


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

CSHARP
int age = 25;
string name = "C#";
age = name;
▶ Try it Yourself

This produces a compile-time error — you cannot assign a string to an int variable:

TEXT
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

CSHARP
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();
▶ Try it Yourself
TEXT
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
💡 Tip: For new projects, prefer .NET 8 LTS. If you want the latest features, try .NET 9. Avoid .NET Framework unless maintaining legacy projects.


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
⚠️ Warning: C# and Java have very similar syntax, but C# evolves faster and features like LINQ, async/await, properties, and operator overloading that Java has long lacked. Compared to C++, C# trades some peak performance for safety and developer productivity. Compared to Python, C# is compiled and strictly typed, making it easier to maintain in large projects.


8. Your First C# Program

▶ Example

Using top-level statements introduced in C# 9.0, you only need one line:

CSHARP
System.Console.WriteLine("Hello, World!");
▶ Try it Yourself
TEXT
Hello, World!

▶ Example

The traditional approach using the Main method entry point (the standard style before C# 9.0):

CSHARP
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
▶ Try it Yourself
TEXT
Hello, World!
💡 Tip: Both approaches are functionally identical. New projects should prefer top-level statements for brevity. Understanding the traditional style helps when reading older code and learning object-oriented structures.


9. How to Master C#

❓ FAQ

Q What's the relationship between C# and .NET?
A C# is the programming language; .NET is the runtime platform. C# code compiles and runs on .NET's CLR, just like a car (C#) needs a road (.NET) to drive on.
Q Do I need Windows to learn C#?
A No. .NET 5+ supports Linux and macOS, and VS Code + C# Dev Kit gives you a full cross-platform development environment.
Q What's the difference between .NET Framework and .NET 8?
A .NET Framework is the older Windows-only version with no new feature development. .NET 8 is the modern cross-platform LTS release, recommended for all new projects.
Q Is Unity's C# the same as standard C#?
A The syntax is identical, but Unity runs on a Mono/.NET Standard subset, so some newer C# features may not yet be supported. Unity also provides a large set of game-specific APIs.

📖 Summary

📝 Exercises

  1. Install the .NET SDK, run dotnet new console -n MyFirstApp in the terminal, then use top-level statements to output your name and a brief self-introduction
  2. Rewrite the top-level statements from exercise 1 in the traditional Program.Main style, and compare the two approaches
  3. Research what LINQ (introduced in C# 3.0) is, and summarize its purpose in one sentence
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏