C# (pronounced "see sharp") is a high-level, statically typed, multi-paradigm programming language developed by Microsoft. C# code usually targets Microsoft's .NET family of tools and run-times, which include .NET, .NET Framework, .NET MAUI, and Xamarin among others. Use this tag for questions about code written in C# or about C#'s formal specification.
c# is a multi-paradigm programming language including object-oriented programming, functional programming, and imperative programming created by Microsoft in conjunction with .NET. C# can be used with any .NET implementation such as .NET, .NET Framework, Mono, ML.Net and Xamarin.
Versions
Version | ECMA¹ | ISO/IEC¹ | Microsoft¹ | Date | .Net Version | Visual Studio |
---|---|---|---|---|---|---|
C# 1.0 | December 2002 | April 2003 | January 2002 | January 2002 | .NET Framework 1.0 | Visual Studio .NET 2002 |
C# 1.1 | October 2003 | April 2003 | .NET Framework 1.1 | Visual Studio .NET 2003 | ||
C# 1.2 | ||||||
C# 2.0 | June 2006 | September 2006 | September 2005 | .NET Framework 2.0 | Visual Studio .NET 2005 | |
.NET Framework 3.0 | Visual Studio .NET 2008 | |||||
C# 3.0 | August 2007 | November 2007 | .NET Framework 2.0 (Except LINQ) | Visual Studio 2008 | ||
.NET Framework 3.0 (Expect LINQ) | ||||||
.NET Framework 3.5 | ||||||
C# 4.0 | April 2010 | April 2010 | .NET Framework 4 | Visual Studio 2010 | ||
C# 5.0 | December 2017 | December 2018 | June 2013 | August 2012 | .NET Framework 4.5 | Visual Studio 2012 |
Visual Studio 2013 | ||||||
Draft | .NET Framework 4.6 | Visual Studio 2015 | ||||
C# 6.0 | July 2015 | .NET Core 1.0 | ||||
.NET Core 1.1 | ||||||
C# 7.0 | ² | March 2017 | .NET Framework 4.7 | Visual Studio 2017 version 15.0 | ||
C# 7.1 | ² | August 2017 | .NET Core 2.0 | Visual Studio 2017 version 15.3 | ||
C# 7.2 | ² | November 2017 | - | Visual Studio 2017 version 15.5 | ||
² | .NET Core 2.1 | Visual Studio 2017 version 15.7 | ||||
C# 7.3 | May 2018 | .NET Core 2.2 | ||||
.NET Framework 4.8 | ||||||
C# 8 | ² | September 2019 | .NET Core 3.0 | Visual Studio 2019 version 16.3 | ||
C# 9.0 | ² | September 2020 | .NET 5.0 | Visual Studio 2019 version 16.8 | ||
C# 10 | ² | November 2021 | .NET 6.0 | Visual Studio 2022 version 17.0 | ||
C# 11 | ² | November 2022 | .NET 7.0 | Visual Studio 2022 version 17.4 |
¹: Language specification
²: Specification proposal
New features
Versions 1.0/1.2 and 2.0 of C# were submitted and approved as both ECMA (Web-Version) and ISO/IEC standards. Latest ECMA version matches Microsoft C# 5.0 specification. Language specifications are also available from Microsoft for C# 3.0 and C# 5.0 as well as C# 6.0 draft.
The language's type system was originally static, with only explicit variable declarations allowed. The introduction of var
(C# 3.0) and dynamic
(C# 4.0) allowed it to use type inference for implicit variable typing, and to consume dynamic type systems, respectively. Delegates, especially with lexical closure support for anonymous methods (C# 2.0) and lambda expressions (C# 3.0), allow the language to be used for functional programming.
C# 5.0 introduced the async
and await
keywords to simplify the use of asynchronous function calls.
C# 6.0 introduced the null propagation operator ?.
, exception filters, string interpolation, and many other features that help to write simple code.
C# 7.0 introduced multiple out
arguments, pattern matching, tuples for a return value, is
-expressions & switch
statements with patterns, deconstruction, local functions, and some more.
C# 7.1 introduced generic Pattern Matching, inferred tuple element names, default
literal expressions, async main, and some more.
C# 7.2 introduced private protected
, non-trailing named arguments, digital separator after base specifier, ref conditional expression, reference semantics for value types, and some more.
C# 7.3 introduced features that enable safe code to be as performant as unsafe code, new compiler options, the usage of out
variable declarations in field, property and constructor initializers, support ==
and !=
on tuple types and some more.
C# 8.0 introduced nullable reference types which generates compiler warnings about possible dereferencing of nulls unless the code expresses explicitly that a variable may be null
(e.g., string? foo
is a variable which may be null), async streams which empowers -especially- IoT and cloud integrations and default interface methods to prevent breaking changes to interfaces, along with some other improvements.
C# 9.0 introduced plenty of new concepts and features such as Records, Init only setters, Top-level statements, Pattern matching enhancements and more.
The compilation is usually done in the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR). However, options like NGen (for the .NET Framework) and AOT (for Mono) mean that C# code can be directly compiled into the native image. Additionally, some frameworks (for example, the .NET Micro Framework) act as CIL interpreters, with no JIT.
Generics in C# are provided in part by the runtime, unlike C++ templates (templates are resolved at compile time), or Java's generics (which use type-erasure).
With the combination of .NET Core for Windows, macOS and Linux, .NET Framework for Windows (desktop/server/mobile), Mono that powers Xamarin applications on Android, Mac, iOS, tvOS and watchOS, Silverlight/Moonlight (browser/mobile), Compact Framework (mobile), and Micro Framework (embedded devices), it's available for a wide range of platforms.
C# 10 introduced file-scoped namespace declarations, global using directives, implicit usings, record structs, along with some other improvements.
C# 11 introduced raw string literals, required members, generic attributes, list patterns, file-local types, generic math support and more.
Hello World Example:
using System;
class Hello
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
Hello World example using classes:
using System;
namespace HelloWorldUsingClasses
{
class Greeting
{
public static void SayHello()
{
Console.WriteLine("Hello World!");
}
}
class Program
{
static void Main(string[] args)
{
Greeting.SayHello();
}
}
}
Hello World example since .NET 6:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Stack Overflow resources
FAQs
- How to send email in ASP.NET C#
- What are the correct version numbers for C#?
- What is the difference between String and string in C#?
- Why is
Random
giving the same results every time? - Captured variables in loops
- In C#, is it possible to cast a
List<Child>
toList<Parent>
? - Does C# pass objects by reference?
- Why does the compiler complain about my conditional expression (
a == b ? x : y
)? - Why do I get a
NullReferenceException
? (Object reference not set to instance of object) - What is an "index out of range" exception, and how do I fix it?
- Hidden Features of C#?
Release Notes
- New Features in C# (Latest)
- New Features in C# 4
- New Features in C# 6
- New Features in C# 7
- New Features in C# 8
- New Features in C# 9
- New Features in C# 10
- New Features in C# 11
- What's new in the .NET Framework?
Resources
- The history of C#
- C# 5.0 Language Specification
- C# 6.0 draft Language Specification
- Wikipedia Article
- .NET Projects on GitHub
- Eric Lippert's old blog || Eric Lippert's new blog
- Jon Skeet's blog
- Programming Guide
- Getting Started with Visual C#
- C# Fundamentals: Development for Absolute Beginners
- Visual C# .NET for Beginners
- csharp.2000things
- Dot Net Perls
Books
- CLR via C#
- C# 9.0 in a Nutshell: The Definitive Reference
- C# in Depth
- Head First C# (3rd Edition, 4th Edition)
- The C# Programming Language (3rd Edition, 4th Edition)
- Framework Design Guidelines (2nd Edition, 3rd Edition)
- Essential C# (6.0 (5th Edition), 7.0 (6th Edition), 8.0 (7th Edition))
- Visual C# How to Program
- Visual C# .NET Step by Step
- C# 5.0 Unleashed
- Pro C# (7.0 (8th Edition), 8.0 (9th Edition), 9.0 (10th Edition))
- Learning C# 3.0
- Starting out with Visual C# (4th Edition, 5th Edition)
- Programming C# (5.0, 8.0)
- C# 4.0 The Complete Reference
- C# Design Pattern Essentials
- C# 6.0 and the .NET 4.6 Framework
- C# 7 and .NET Core
- Effective C# (Covers C# 6.0), 50 Specific Ways to Improve Your C# (3rd Edition)
- C# 7 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library (2nd Edition)
Tutorials
- MSDN
- Microsoft Virtual Academy
- learn.microsoft.com
- microsoft.com/dotnet
- Pluralsight C# path
- exercism.org
Future of C# language
Contribute and propose new features here.
Demo Projects
C# Online IDE
C# Offline IDE