Introduction to C#
C# (pronounced “C-Sharp”) is a modern, general-purpose, object-oriented programming language developed by Microsoft as part of the .NET platform. It is designed for building a wide range of applications — from web APIs and cloud services to desktop apps, games (Unity), and mobile apps (MAUI).
What is C#?
C# combines the power of C++ with the simplicity of Java and the productivity of Visual Basic. It runs on the Common Language Runtime (CLR), which provides garbage collection, type safety, and exception handling. Code is compiled to Intermediate Language (IL) and executed by the runtime, enabling cross-platform deployment through .NET.
Key Features of C#
- Object-Oriented: Classes, inheritance, interfaces, and polymorphism are first-class concepts.
- Type-Safe: The compiler catches type errors at build time; nullable reference types (C# 8+) reduce null-related bugs.
- Modern Syntax: Pattern matching, records, LINQ, async/await, and top-level statements keep code concise.
- Rich Standard Library: The .NET Base Class Library (BCL) covers I/O, networking, collections, and cryptography.
- Interoperability: P/Invoke calls native libraries; COM interop works on Windows.
- Garbage Collection: Automatic memory management reduces manual allocation errors.
C# vs Other Languages
| Aspect | C# | Java | Python |
|---|---|---|---|
| Typing | Static, strong | Static, strong | Dynamic |
| Runtime | .NET CLR | JVM | CPython interpreter |
| Performance | High (JIT/AOT) | High (JIT) | Moderate |
| Primary use | Enterprise, games, web | Enterprise, Android | Scripting, data science |
C# and Java share similar syntax and OOP models. C# tends to evolve faster (records, pattern matching, source generators). Python trades performance for readability and is better suited to scripting and ML pipelines.
The .NET Ecosystem
- .NET SDK — compiler, runtime, and CLI tools (
dotnetcommand). - ASP.NET Core — cross-platform web framework for REST APIs and MVC apps.
- Entity Framework Core — ORM for SQL Server, PostgreSQL, SQLite, and more.
- Blazor — build interactive web UIs in C# instead of JavaScript.
- Unity — game engine using C# for scripting.
Your First C# Program
Create a project and run it:
dotnet new console -o HelloCSharp
cd HelloCSharp
dotnet run
The default template looks like this:
// Program.cs
Console.WriteLine("Hello, C#!");
A class-based equivalent:
using System;
namespace HelloCSharp
{
class Program
{
static void Main(string[] args)
{
string name = args.Length > 0 ? args[0] : "World";
Console.WriteLine($"Hello, {name}!");
}
}
}
Run with an argument:
dotnet run -- Simon
# Output: Hello, Simon!
Variables and Types
C# is statically typed. Common value and reference types:
int age = 30;
double price = 19.99;
string title = "GazeHub";
bool isActive = true;
// var lets the compiler infer the type
var items = new List<string> { "C#", ".NET", "ASP.NET" };
foreach (var item in items)
{
Console.WriteLine(item);
}
Object-Oriented Example
public record User(int Id, string Name, string Email);
public class UserService
{
private readonly List<User> _users = new();
public void Add(User user) => _users.Add(user);
public User? FindById(int id) =>
_users.FirstOrDefault(u => u.Id == id);
}
var service = new UserService();
service.Add(new User(1, "Alice", "[email protected]"));
Console.WriteLine(service.FindById(1)?.Name); // Alice
When to Choose C#
- Enterprise web APIs — ASP.NET Core is fast, well-documented, and integrates with Azure.
- Windows desktop apps — WPF and WinForms remain strong choices.
- Cross-platform mobile — .NET MAUI targets iOS, Android, macOS, and Windows.
- Game development — Unity uses C# as its primary scripting language.
- Cloud and microservices — Docker, Kubernetes, and Azure deploy .NET services easily.
Development Tools
- Visual Studio — full IDE for Windows and Mac with debugging, profiling, and Azure integration.
- Visual Studio Code — lightweight editor with the C# Dev Kit extension.
- JetBrains Rider — cross-platform IDE with excellent refactoring support.
Install the .NET SDK from https://dotnet.microsoft.com/download, then follow the Hello World tutorial in this track.
What Comes Next
This track progresses from syntax and data types through OOP, collections, ASP.NET Core, Entity Framework, dependency injection, and testing. By the end you will be able to design, build, and deploy production .NET applications.