Questions tagged [c#]

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.

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


Release Notes


Resources


Books


Tutorials


Future of C# language

Contribute and propose new features here.


Demo Projects


C# Online IDE


C# Offline IDE

Related tags

1601398 questions
7486
votes
66 answers

What is the difference between String and string in C#?

What are the differences between these two, and which one should I use? string s = "Hello world!"; String s = "Hello world!";
Lance Fisher
  • 25,684
  • 22
  • 96
  • 122
4339
votes
34 answers

How to enumerate an enum?

How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { …
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
3856
votes
32 answers

How do I cast int to enum in C#?

How do I cast an int to an enum in C#?
lomaxx
  • 113,627
  • 57
  • 144
  • 179
3384
votes
31 answers

Case insensitive 'Contains(string)'

Is there a way to make the following return true? string title = "ASTRINGTOTEST"; title.Contains("string"); There doesn't seem to be an overload that allows me to set the case sensitivity. Currently I UPPERCASE them both, but that's just silly (by…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
3252
votes
20 answers

How to iterate over a dictionary?

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
Jake Stewart
  • 32,729
  • 3
  • 20
  • 17
2876
votes
13 answers

What are the correct version numbers for C#?

What are the correct version numbers for C#? What came out when? Why can't I find any answers about C# 3.5? This question is primarily to aid those who are searching for an answer using an incorrect version number, e.g. C# 3.5. The hope is that…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2611
votes
59 answers

Deep cloning objects

I want to do something like: MyObject myObj = GetMyObj(); // Create and fill a new object MyObject newObj = myObj.Clone(); And then make changes to the new object that are not reflected in the original object. I don't often need this functionality,…
NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
2540
votes
29 answers

Catch multiple exceptions at once?

It is discouraged to simply catch System.Exception. Instead, only the "known" exceptions should be caught. Now, this sometimes leads to unnecessary repetitive code, for example: try { WebId = new Guid(queryString["web"]); } catch…
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
2386
votes
15 answers

Should 'using' directives be inside or outside the namespace in C#?

I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace. Is there a technical reason for putting the using directives inside instead of outside the namespace?
benPearce
  • 37,735
  • 14
  • 62
  • 96
2377
votes
33 answers

How do I generate a random integer in C#?

How do I generate a random integer in C#?
Rella
  • 65,003
  • 109
  • 363
  • 636
2356
votes
41 answers

How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

How do I convert a string to a byte[] in .NET (C#) without manually specifying a specific encoding? I'm going to encrypt the string. I can encrypt it without converting, but I'd still like to know why encoding comes to play here. Also, why should…
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
2314
votes
31 answers

Get int value from enum in C#

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this. public enum Question { Role = 2, ProjectFunding = 3, TotalEmployee = 4, NumberOfServers = 5, …
jim
  • 26,598
  • 13
  • 51
  • 66
2309
votes
23 answers

What is the best way to give a C# auto-property an initial value?

How do you give a C# auto-property an initial value? I either use the constructor, or revert to the old syntax. Using the Constructor: class Person { public Person() { Name = "Initial Name"; } public string Name { get;…
bentford
  • 33,038
  • 7
  • 61
  • 57
2220
votes
74 answers

How do I calculate someone's age based on a DateTime type birthday?

Given a DateTime representing a person's birthday, how do I calculate their age in years?
Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
2146
votes
47 answers

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

How can I create an Excel spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
mistrmark
  • 4,349
  • 6
  • 22
  • 15
1
2 3
99 100