Design goals
The Ecma standard lists these design goals for C#: * The language is intended to be a simple, modern, general-purpose, object-oriented programming language. * The language, and implementations thereof, should provide support for software engineering principles such as strong type checking, array bounds checking, detection of attempts to useHistory
During the development of the .NET Framework, the class libraries were originally written using a managed code compiler system named ''Simple Managed C'' (SMC). In January 1999, Anders Hejlsberg formed a team to build a new language at the time called COOL, which stood for " C-like Object Oriented Language". Microsoft had considered keeping the name "COOL(C-like Object Oriented Language)" as the final name of the language, but chose not to do so for trademark reasons. By the time the .NET project was publicly announced at the July 2000 Professional Developers Conference, the language had been renamed C#, and the class libraries and ASP.NET runtime were ported to C#. Hejlsberg is C#'s principal designer and lead architect at Microsoft, and was previously involved with the design of Turbo Pascal, EmbarcaderoName
Versions
Development of the text for standards (beginning with C# 6) is done oSyntax
The core syntax of the C# language is similar to that of other C-style languages such as C, Objective-C, C++ and Java, particularly: * Semicolons are used to denote the end of a statement. * Curly brackets are used to group statements. Statements are commonly grouped into methods (functions), methods into classes, and classes into namespaces. * Variables are assigned using anDistinguishing features
Some notable features of C# that distinguish it from C, C++, and Java where noted, are:Portability
By design, C# is the programming language that most directly reflects the underlyingTyping
C# supports strongly, implicitly typed variable declarations with the keywordvar
, and implicitly typed arrays with the keyword new[]
followed by a collection initializer.
Its type system is split into two families: Value types, like the built-in numeric types and user-defined structs, which are automatically handed over as copies when used as parameters, and reference types, including arrays, instances of classes, and strings, which only hand over a pointer to the respective object. Due to their special handling of the equality operator and their immutability, strings will nevertheless behave as if they were values, for all practical purposes. You can even use them as case labels. Where necessary, value types will be boxed automatically.
C# supports a strict Boolean data type, bool
. Statements that take conditions, such as while
and if
, require an expression of a type that evaluates to the true
boolean value. While C++ also has a Boolean type, it can be freely converted to and from integers, and expressions such as if (a)
require only that a
is convertible to bool, allowing a
to be an int, or a pointer. C# disallows this "integer meaning true or false" approach, on the grounds that forcing programmers to use expressions that return exactly bool
can prevent certain types of programming mistakes such as if (a = b)
(use of assignment =
instead of equality
).
C# is more type safe than C++. The only implicit conversions by default are those that are considered safe, such as widening of integers. This is enforced at compile-time, during JIT, and, in some cases, at runtime. No implicit conversions occur between Booleans and integers, nor between enumeration members and integers (except for literal 0, which can be implicitly converted to any enumerated type). Any user-defined conversion must be explicitly marked as explicit or implicit, unlike C++ copy constructors and conversion operators, which are both implicit by default.
C# has explicit support for covariance and contravariance in generic types, unlike C++ which has some degree of support for contravariance simply through the semantics of return types on virtual methods.
Metaprogramming
Metaprogramming can be achieved in several ways: * Reflection is supported through .NET APIs, which enable scenarios such as type metadata inspection and dynamic method invocation. * Expression trees represent code as an abstract syntax tree, where each node is an expression that can be inspected or executed. This enables dynamic modification of executable code at runtime. Expression trees introduce someSystem.Reflection.Emit
namespace, which contains classes that emit metadata and CIL (types, assemblies, etc.) at runtime.
* The .NET Compiler Platform (Roslyn) provides API access to language compilation services, allowing for the compilation of C# code from within .NET applications. It exposes APIs for syntactic ( lexical) analysis of code, semantic analysis, dynamic compilation to CIL, and code emission.
* Source generators, a feature of the Roslyn C# compiler, enable compile time metaprogramming. During the compilation process, developers can inspect the code being compiled with the compiler's API and pass additional generated C# source code to be compiled.
Methods and functions
A ''method'' in C# is a member of a class that can be invoked as a function (a sequence of instructions), rather than the mere value-holding capability of a ''field'' (i.e.private
), the explicit specification of its return type (such as int
, or the keyword void
if no value is returned), the name of the method, and finally, a parenthesized sequence of comma-separated parameter specifications, each consisting of a parameter's type, its formal name and optionally, a default value to be used whenever none is provided. Different from most other languages, call-by-reference parameters have to be marked both at the function definition and at the calling site, and you can choose between ref
and out
, the latter allowing handing over an uninitialized variable which will have a definite value on return. Additionally, you can specify a variable-sized argument list by applying the params
keyword to the last parameter. Certain specific kinds of methods, such as those that simply get or set a field's value by returning or assigning it, do not require an explicitly stated full signature, but in the general case, the definition of a class includes the full signature declaration of its methods.
Like C++, and unlike Java, C# programmers must use the scope modifier keyword virtual
to allow methods to be overridden by subclasses. Unlike C++, you have to explicitly specify the keyword override
when doing so. This is supposed to avoid confusion between overriding and newly overloading a function (i.e. hiding the former implementation). To do the latter, you have to specify the new
keyword. You can use the keyword sealed
to disallow further overrides for individual methods or whole classes.
''Extension methods'' in C# allow programmers to use static methods as if they were methods from a class's method table, allowing programmers to virtually add instance methods to a class that they feel should exist on that kind of objects (and instances of the respective derived classes).
The type dynamic
allows for run-time method binding, allowing for JavaScript-like method calls and run-time object composition.
C# has support for strongly-typed delegate
. Like the Qt framework's pseudo-C++ ''signal'' and ''slot'', C# has semantics specifically surrounding publish-subscribe style events, though C# uses delegates to do so.
C# offers Java-like synchronized
method calls, via the attribute ethodImpl(MethodImplOptions.Synchronized)/code>, and has support for mutually-exclusive locks via the keyword lock
.
Property
C# supports classes with properties
Property is the ownership of land, resources, improvements or other tangible objects, or intellectual property.
Property may also refer to:
Philosophy and science
* Property (philosophy), in philosophy and logic, an abstraction characterizing an ...
. The properties can be simple accessor functions with a backing field, or implement arbitrary getter and setter functions. A property is read-only if there's no setter. Like with fields, there can be class and instance properties. The underlying methods can be virtual
or abstract
like any other method.
Since C# 3.0 the syntactic sugar of auto-implemented properties is available, where the accessor (getter) and mutator (setter) encapsulate operations on a single field of a class.
Namespace
A C# namespace
provides the same level of code isolation as a Java package
or a C++ , with very similar rules and features to a package
. Namespaces can be imported with the "using" syntax.
Memory access
In C#, memory address pointers can only be used within blocks specifically marked as ''unsafe'', and programs with unsafe code need appropriate permissions to run. Most object access is done through safe object references, which always either point to a "live" object or have the well-defined null
Null may refer to:
Science, technology, and mathematics Astronomy
*Nuller, an optical tool using interferometry to block certain sources of light Computing
*Null (SQL) (or NULL), a special marker and keyword in SQL indicating that a data value do ...
value; it is impossible to obtain a reference to a "dead" object (one that has been garbage collected), or to an arbitrary block of memory. An unsafe pointer can point to an instance of an unmanaged value type that does not contain any references to objects subject to garbage collections such as class instances, arrays or strings. Code that is not marked as unsafe can still store and manipulate pointers through the System.IntPtr
type, but it cannot dereference them.
Managed memory cannot be explicitly freed; instead, it is automatically garbage collected. Garbage collection addresses the problem of memory leaks by freeing the programmer of responsibility for releasing memory that is no longer needed in most cases. Code that retains references to objects longer than is required can still experience higher memory usage than necessary, however once the final reference to an object is released the memory is available for garbage collection.
Exceptions
A range of standard exceptions are available to programmers. Methods in standard libraries regularly throw system exceptions in some circumstances and the range of exceptions thrown is normally documented. Custom exception classes can be defined for classes allowing handling to be put in place for particular circumstances as needed.
The syntax for handling exceptions is the following:try
catch (Exception ex)
finally
Most of the time people call this a "try-catch" code block, because of the "try" and "catch" functions being used and accessible on all C# versions.
try
catch (Exception ex)
finally
Depending on your plans, the "finally" part can be left out. If error handling is not required, the (Exception ex)
parameter can be omitted as well. Also, there can be several "catch" parts handling different kinds of exceptions.
Checked exceptions are not present in C# (in contrast to Java). This has been a conscious decision based on the issues of scalability and version management.
Polymorphism
Unlike C++, C# does not support multiple inheritance, although a class can implement any number of " interfaces" (fully abstract classes). This was a design decision by the language's lead architect to avoid complications and to simplify architectural requirements throughout CLI.
When implementing multiple interfaces that contain a method with the same name and taking parameters of the same type in the same order (i.e. the same signature
A signature (; from , "to sign") is a depiction of someone's name, nickname, or even a simple "X" or other mark that a person writes on documents as a proof of identity and intent. Signatures are often, but not always, Handwriting, handwritt ...
), similar to Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, C# allows both a single method to cover all interfaces and if necessary specific methods for each interface.
C# also offers function overloading (a.k.a. ad-hoc-polymorphism), i.e. methods with the same name, but distinguishable signatures. Unlike Java, C# additionally supports operator overloading.
Since version 2.0, C# offers parametric polymorphism, i.e. classes with arbitrary or constrained type parameters, e.g. List
, a variable-sized array which only can contain elements of type T
. There are certain kinds of constraints you can specify for the type parameters: Has to be type X ( or one derived from it), has to implement a certain interface, has to be a reference type, has to be a value type, has to implement a public parameterless constructor. Most of them can be combined, and you can specify any number of interfaces.
Language Integrated Query (LINQ)
C# has the ability to utilize LINQ through the .NET Framework. A developer can query a variety of data sources, provided the IEnumerable<T>
interface is implemented on the object. This includes XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
documents, an ADO.NET dataset, and SQL databases.
Using LINQ in C# brings advantages like IntelliSense support, strong filtering capabilities, type safety with compile error checking ability, and consistency for querying data over a variety of sources. There are several different language structures that can be utilized with C# and LINQ and they are query expressions, lambda expressions, anonymous types, implicitly typed variables, extension methods, and object initializers.
LINQ has two syntaxes: query syntax and method syntax. However, the compiler always converts the query syntax to method syntax at compile time.
using System.Linq;
var numbers = new int[] ;
// Query syntax (SELECT num FROM numbers WHERE num % 2 = 0 ORDER BY num)
var numQuery1 =
from num in numbers
where num % 2 0
orderby num
select num;
// Method syntax
var numQuery2 =
numbers
.Where(num => num % 2 0)
.OrderBy(n => n);
Functional programming
Though primarily an imperative language, C# always adds functional features over time, for example:
* Functions as first-class citizen – C# 1.0 delegates
* Higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:
* takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
s – C# 1.0 together with delegates
* Anonymous functions – C# 2 anonymous delegates and C# 3 lambdas expressions
* Closures – C# 2 together with anonymous delegates and C# 3 together with lambdas expressions
* Type inference – C# 3 with implicitly typed local variables and C# 9 target-typed new expressions
* List comprehension – C# 3 LINQ
* Tuple
In mathematics, a tuple is a finite sequence or ''ordered list'' of numbers or, more generally, mathematical objects, which are called the ''elements'' of the tuple. An -tuple is a tuple of elements, where is a non-negative integer. There is o ...
s – .NET Framework 4.0 but it becomes popular when C# 7.0 introduced a new tuple type with language support
* Nested functions – C# 7.0
* Pattern matching – C# 7.0
* Immutability – C# 7.2 readonly struct C# 9 record types and Init only setters
* Type classes – C# 12 roles/extensions (in development)
Common type system
C# has a ''unified type system''. This unified type system is called Common Type System (CTS).
A unified type system implies that all types, including primitives such as integers, are subclasses of the class. For example, every type inherits a method.
Categories of data types
CTS separates data types into two categories:
# Reference types
# Value types
Instances of value types neither have referential identity nor referential comparison semantics. Equality and inequality comparisons for value types compare the actual data values within the instances, unless the corresponding operators are overloaded. Value types are derived from , always have a default value, and can always be created and copied. Some other limitations on value types are that they cannot derive from each other (but can implement interfaces) and cannot have an explicit default (parameterless) constructor because they already have an implicit one which initializes all contained data to the type-dependent default value (0, null, or alike). Examples of value types are all primitive types, such as (a signed 32-bit integer), (a 32-bit IEEE floating-point number), (a 16-bit Unicode code unit), decimal
(fixed-point numbers useful for handling currency amounts), and (identifies a specific point in time with nanosecond precision). Other examples are (enumerations) and (user defined structures).
In contrast, reference types have the notion of referential identity, meaning that each instance of a reference type is inherently distinct from every other instance, even if the data within both instances is the same. This is reflected in default equality and inequality comparisons for reference types, which test for referential rather than structural equality, unless the corresponding operators are overloaded (such as the case for ). Some operations are not always possible, such as creating an instance of a reference type, copying an existing instance, or performing a value comparison on two existing instances. Nevertheless, specific reference types can provide such services by exposing a public constructor or implementing a corresponding interface (such as or ). Examples of reference types are (the ultimate base class for all other C# classes), (a string of Unicode characters), and (a base class for all C# arrays).
Both type categories are extensible with user-defined types.
Boxing and unboxing
''Boxing'' is the operation of converting a value-type object into a value of a corresponding reference type. Boxing in C# is implicit.
''Unboxing'' is the operation of converting a value of a reference type (previously boxed) into a value of a value type. Unboxing in C# requires an explicit type cast. A boxed object of type T can only be unboxed to a T (or a nullable T).
Example:
int foo = 42; // Value type.
object bar = foo; // foo is boxed to bar.
int foo2 = (int)bar; // Unboxed back to value type.
Libraries
The C# specification details a minimum set of types and class libraries that the compiler expects to have available. In practice, C# is most often used with some implementation of the Common Language Infrastructure
The Common Language Infrastructure (CLI) is an open specification and technical standard originally developed by Microsoft and standardized by International Organization for Standardization, ISO/International Electrotechnical Commission, IEC (ISO/ ...
(CLI), which is standardized as ECMA-335 ''Common Language Infrastructure (CLI)''.
In addition to the standard CLI specifications, there are many commercial and community class libraries that build on top of the .NET framework libraries to provide additional functionality.
C# can make calls to any library included in the List of .NET libraries and frameworks.
Examples
Hello World
The following is a very simple C# program, a version of the classic " Hello world" example using th
top-level statements
feature introduced in C# 9:
System.Console.WriteLine("Hello, world!");
For code written as C# 8 or lower, the entry point logic of a program must be written in a Main method inside a type:
using System;
class Program
This code will display this text in the console window:
Hello, world!
Each line has a purpose:
using System;
The above line imports all types in the System
namespace. For example, the Console
class used later in the source code is defined in the System
namespace, meaning it can be used without supplying the full name of the type (which includes the namespace).
// A version of the classic "Hello World" program
This line is a comment; it describes and documents the code for the programmer(s).
class Program
Above is a class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
definition for the class. Everything that follows between the pair of braces describes that class.
The curly brackets demarcate the boundaries of a code block. In this first instance, they are marking the start and end of the class.
static void Main()
This declares the class member method where the program begins execution. The .NET runtime calls the method. Unlike in Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, the method does not need the keyword, which tells the compiler that the method can be called from anywhere by any class. Writing is equivalent to writing . The static keyword makes the method accessible without an instance of . Each console application's entry point must be declared otherwise the program would require an instance of , but any instance would require a program. To avoid that irresolvable circular dependency, C# compilers processing console applications (like that above) report an error if there is no method. The keyword declares that has no return value. (Note, however, that short programs can be written usin
Top Level Statements
introduced in C# 9, as mentioned earlier.)
Console.WriteLine("Hello, world!");
This line writes the output. is a static class in the namespace. It provides an interface to the standard input/output
In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs a ...
, and error streams for console applications. The program calls the method , which displays on the console a line with the argument, the string .
Generics
With .NET 2.0 and C# 2.0, the community got more flexible collections than those in .NET 1.x. In the absence of generics, developers had to use collections such as ArrayList to store elements as objects of unspecified kind, which incurred performance overhead when boxing/unboxing/type-checking the contained items.
Generics introduced a massive new feature in .NET that allowed developers to create type-safe data structures. This shift is particularly important in the context of converting legacy systems, where updating to generics can significantly enhance performance and maintainability by replacing outdated data structures with more efficient, type-safe alternatives.
Example
public class DataStore
Standardization and licensing
In August 2001, Microsoft
Microsoft Corporation is an American multinational corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
, Hewlett-Packard
The Hewlett-Packard Company, commonly shortened to Hewlett-Packard ( ) or HP, was an American multinational information technology company. It was founded by Bill Hewlett and David Packard in 1939 in a one-car garage in Palo Alto, California ...
and Intel
Intel Corporation is an American multinational corporation and technology company headquartered in Santa Clara, California, and Delaware General Corporation Law, incorporated in Delaware. Intel designs, manufactures, and sells computer compo ...
co-sponsored the submission of specifications for C# as well as the Common Language Infrastructure (CLI) to the standards organization Ecma International
Ecma International () is a Nonprofit organization, nonprofit standards organization for information and communication systems. It acquired its current name in 1994, when the European Computer Manufacturers Association (ECMA) changed its name to ...
. In December 2001, ECMA released ECMA-334 ''C# Language Specification''. C# became an ISO
The International Organization for Standardization (ISO ; ; ) is an independent, non-governmental, international standard development organization composed of representatives from the national standards organizations of member countries.
Me ...
/ IEC standard in 2003 (ISO/IEC 23270:2003 - ''Information technology — Programming languages — C#''). ECMA had previously adopted equivalent specifications as the 2nd edition of C#, in December 2002. In June 2005, ECMA approved edition 3 of the C# specification, and updated ECMA-334. Additions included partial classes, anonymous methods, nullable types, and generics (somewhat similar to C++ templates). In July 2005, ECMA submitted to ISO/IEC JTC 1/SC 22, via the latter's Fast-Track process, the standards and related TRs. This process usually takes 6–9 months.
The C# language definition and the CLI are standardized under ISO
The International Organization for Standardization (ISO ; ; ) is an independent, non-governmental, international standard development organization composed of representatives from the national standards organizations of member countries.
Me ...
/ IEC and Ecma standards that provide reasonable and non-discriminatory licensing protection from patent claims.
Microsoft initially agreed not to sue open-source developers for violating patents in non-profit projects for the part of the framework that is covered by the Open Specification Promise. Microsoft has also agreed not to enforce patents relating to Novell
Novell, Inc. () was an American software and services company headquartered in Provo, Utah, that existed from 1980 until 2014. Its most significant product was the multi-platform network operating system known as NetWare. Novell technolog ...
products against Novell's paying customers with the exception of a list of products that do not explicitly mention C#, .NET or Novell's implementation of .NET ( The Mono Project). However, Novell maintained that Mono does not infringe any Microsoft patents. Microsoft also made a specific agreement not to enforce patent rights related to the Moonlight browser plugin, which depends on Mono, provided it is obtained through Novell.
A decade later, Microsoft began developing free, open-source, and cross-platform tooling for C#, namely Visual Studio Code, .NET Core, and Roslyn. Mono joined Microsoft as a project of Xamarin, a Microsoft subsidiary.
Implementations
Microsoft has developed open-source reference C# compilers and tools. The first compiler, Roslyn, compiles into intermediate language (IL), and the second one, RyuJIT, is a JIT (just-in-time) compiler, which is dynamic and does on-the-fly optimization and compiles the IL into native code for the front-end of the CPU. RyuJIT is open source and written in C++. Roslyn is entirely written in managed code (C#), has been opened up and functionality surfaced as APIs. It is thus enabling developers to create refactoring and diagnostics tools. Two branches of official implementation are .NET Framework (closed-source, Windows-only) and .NET Core (open-source, cross-platform); they eventually converged into one open-source implementation: .NET 5.0. At .NET Framework 4.6, a new JIT compiler replaced the former.
Other C# compilers (some of which include an implementation of the Common Language Infrastructure
The Common Language Infrastructure (CLI) is an open specification and technical standard originally developed by Microsoft and standardized by International Organization for Standardization, ISO/International Electrotechnical Commission, IEC (ISO/ ...
and .NET class libraries):
* Mono, a Microsoft-sponsored project provides an open-source C# compiler, a complete open-source implementation of the CLI (including the required framework libraries as they appear in the ECMA specification,) and a nearly complete implementation of the NET class libraries up to .NET Framework 3.5.
* The Elements tool chain from RemObjects includes RemObjects C#, which compiles C# code to .NET's Common Intermediate Language, Java bytecode, Cocoa, Android bytecode, WebAssembly, and native machine code for Windows, macOS, and Linux.
*The DotGNU project (now discontinued) also provided an open-source C# compiler, a nearly complete implementation of the Common Language Infrastructure including the required framework libraries as they appear in the ECMA specification, and subset of some of the remaining Microsoft proprietary .NET class libraries up to .NET 2.0 (those not documented or included in the ECMA specification, but included in Microsoft's standard .NET Framework distribution).
The Unity game engine uses C# as its primary scripting language. The Godot game engine has implemented an optional C# module due to a donation of $24,000 from Microsoft.
See also
;C# topics
* C# syntax
* Comparison of C# and Java
* Comparison of C# and Visual Basic .NET
* .NET standard libraries
;IDEs
* Visual Studio
* Visual Studio Code
* Rider
* LINQPad
* MonoDevelop
* Morfik
* SharpDevelop
* Turbo C#
* Microsoft Visual Studio Express
* Xamarin Studio
Notes
References
Citations
Sources
*
*
*
*
Further reading
*
*
External links
C# Language Specification
C# Programming Guide
ISO C# Language Specification
C# Compiler Platform ("Roslyn") source code
{{Authority control
2000 software
American inventions
Programming languages
High-level programming languages
.NET programming languages
Class-based programming languages
Ecma standards
Functional languages
IEC standards
ISO standards
Microsoft programming languages
Multi-paradigm programming languages
Programming languages created in 2000
Programming languages with an ISO standard
Statically typed programming languages
Compiled programming languages
Articles with example C Sharp code