C#
- Pronunciation
- SEE-SHARP
In short
C# is a statically typed, object-oriented language that runs on the .NET runtime with garbage collection and is used for web backends, desktop apps, and games.
What is C#?
C# (pronounced "C sharp") is a general-purpose programming language designed at Microsoft by a team led by Anders Hejlsberg, announced in 2000 and released in 2002 as part of the .NET platform. It is mainly object-oriented but has absorbed many functional programming ideas, such as lambdas, pattern matching, and LINQ, a built-in query syntax for filtering and transforming collections. Since 2016, modern .NET has been open source and cross-platform, running on Windows, macOS, and Linux.
C# is statically typed, with local type inference through the var keyword and optional nullable reference types that warn you at compile time when a value might be null. The compiler turns C# into Intermediate Language (IL), a bytecode that the .NET runtime compiles into native machine code with a just-in-time (JIT) compiler, or ahead of time when needed. Memory is managed by a garbage collector, and C# distinguishes reference types (class), which are shared, from value types (struct), which are copied.
C# is used for web backends and APIs, desktop applications, cloud services, business software, and cross-platform mobile apps. It is also a common scripting language inside game engines, where game logic is written in C# while the engine itself runs in native code. The async and await keywords, which many other languages later adopted, were popularized by C#.
Despite the name, C# has more in common with Java than with C or C++. Like Java, it is statically typed, object-oriented, garbage-collected, and compiled to bytecode for a managed runtime. The "#" comes from the musical sharp sign, which raises a note by a half step, hinting at a step up from C++.
Key takeaways
- C# is statically typed and object-oriented, with many functional features.
- Code compiles to Intermediate Language (IL) that the .NET runtime turns into machine code.
- Memory is managed by a garbage collector.
- It is used for web backends, desktop apps, cloud services, and game scripting.
- Despite the name, C# has more in common with Java than with C or C++.
Example
// Top-level statements: no Main method needed in modern C#
var numbers = new List<int> { 5, 12, 8, 21, 3 };
// LINQ filters and sorts collections in a query-like style
var bigOnes = numbers.Where(n => n > 6).OrderBy(n => n);
foreach (var n in bigOnes)
{
Console.WriteLine($"Found {n}");
}Readers ask
Is C# only for Windows?
No. Modern .NET is open source and cross-platform, so C# programs can run on Windows, macOS, and Linux, including in containers and cloud environments.
What is the difference between C# and Java?
They are similar in design: both are statically typed, object-oriented, garbage-collected languages that run on a managed runtime. They differ in their ecosystems and in details such as C#'s custom value types (struct), properties, and LINQ.
Is C# related to C++?
Only loosely. C# borrows C-style syntax, such as curly braces and semicolons, but it is a separate language with automatic memory management and a managed runtime.
See also
- JavaProgramming Languages, p. 15Java is a statically typed, object-oriented programming language that compiles to bytecode for the Java Virtual Machine, so programs run on many platforms.
- C++Programming Languages, p. 5C++ is a compiled, statically typed language that extends C with classes and templates while keeping low-level control over memory and performance.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- Async/AwaitProgramming Fundamentals, p. 4Async/await is a syntax for writing asynchronous code that reads like ordinary step-by-step code, pausing at each await until a promise or task finishes.
- GenericsProgramming Fundamentals, p. 24Generics are a language feature that lets you write functions, classes, and types that work with many data types while still keeping full type safety.
- .NETBackend & APIs, p. 1.NET is Microsoft's free, open-source developer platform for building web, desktop, mobile, cloud and game applications, mainly in C#.
- Visual BasicProgramming Languages, p. 32Visual Basic is Microsoft's family of beginner-friendly languages, including classic Visual Basic, VB.NET and VBA, which automates Office apps with macros.
Spotted a mistake or something missing on this page?Suggest an edit