C# Introduction
C# (pronounced "C sharp") is a general-purpose, object-oriented programming language created by Microsoft. It runs on .NET, a free, open-source and cross-platform runtime that works on Windows, Linux and macOS. C# combines the familiar curly-brace syntax of C, C++ and Java with modern features such as type inference, pattern matching, LINQ queries and async/await.
What is C# used for?
- Desktop applications (WPF, WinForms, .NET MAUI)
- Web sites and web APIs with ASP.NET Core
- Games — C# is the scripting language of the Unity engine
- Cloud services and serverless functions
- Mobile apps for Android and iOS
- Command-line tools and automation scripts
Why learn C#?
| Feature | What it means for you |
|---|---|
| Statically typed | Many mistakes are caught by the compiler before the program runs |
| Garbage collected | Memory is freed automatically, so you rarely manage it by hand |
| Huge standard library | Collections, files, networking, JSON, dates and more are built in |
| Modern syntax | Records, pattern matching and lambdas keep code short and readable |
| Great tooling | Visual Studio, VS Code and Rider give rich autocompletion and debugging |
Your first C# program
Every C# program needs a starting point. In the classic style that is a static void Main() method inside a class. Press ▶ Run to compile and run it, then change the text and run it again.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}Hello, World!
Here is what each part does:
using System;lets us writeConsoleinstead of the full nameSystem.Console.class Programdeclares a class — in C#, all methods live inside a type.static void Main()is the entry point: the runtime calls it when the program starts.Console.WriteLine(...)prints text followed by a new line.
Tip: In every playground on ExcelTAD you can edit the code and press Ctrl+Enter to run it.
Top-level statements
Since C# 9 you can skip the class and Main boilerplate. Statements written directly at the top of a single file become the program's entry point automatically. The compiler generates the hidden Main method for you.
using System;
Console.WriteLine("C# is concise.");
Console.WriteLine("C# is type-safe.");
Console.WriteLine("C# is fun!");C# is concise.
C# is type-safe.
C# is fun!
Both styles produce exactly the same kind of program. This tutorial uses whichever is clearer for the example: top-level statements for short snippets, and an explicit class Program when we also define other methods and classes.
Note: Project templates created with
dotnet new consoleenable implicit usings, sousing System;is added for you. The playgrounds on this site compile a single file without that feature, which is why every example starts withusing System;.
C# compared with similar languages
| C# | Java | C++ | |
|---|---|---|---|
| Memory | Garbage collector | Garbage collector | Manual / RAII |
| Runs on | .NET runtime | JVM | Native machine code |
| Properties | Built into the language | Getter/setter methods | Getter/setter methods |
| Value types | struct, user-definable |
Primitives only | Everything |
If you already know Java you will feel at home quickly; if you know C++ you will appreciate not having to free memory yourself.
Test yourself
Which method is the classic entry point of a C# program?
- Start()
- Run()
- Main()
- Init()
What does using System; allow you to do?
- Use
Consolewithout writingSystem.Console - Run the program faster
- Enable garbage collection
- Import a third-party package
Which runtime does C# code run on?
- The JVM
- Node.js
- .NET
- CPython