Katana VentraIP

C Sharp (programming language)

C# (/ˌs ˈʃɑːrp/ see SHARP)[b] is a general-purpose high-level programming language supporting multiple paradigms. C# encompasses static typing,[16]: 4  strong typing, lexically scoped, imperative, declarative, functional, generic,[16]: 22  object-oriented (class-based), and component-oriented programming disciplines.[17]

The C# programming language was designed by Anders Hejlsberg from Microsoft in 2000 and was later approved as an international standard by Ecma (ECMA-334) in 2002 and ISO/IEC (ISO/IEC 23270 and 20619[c]) in 2003. Microsoft introduced C# along with .NET Framework and Visual Studio, both of which were closed-source. At the time, Microsoft had no open-source products. Four years later, in 2004, a free and open-source project called Mono began, providing a cross-platform compiler and runtime environment for the C# programming language. A decade later, Microsoft released Visual Studio Code (code editor), Roslyn (compiler), and the unified .NET platform (software framework), all of which support C# and are free, open-source, and cross-platform. Mono also joined Microsoft but was not merged into .NET.


As of November 2023, the most recent stable version of the language is C# 12.0, which was released in 2023 in .NET 8.0.[18][19]

The language is intended to be a simple, modern, general-purpose, programming language.

object-oriented

The language, and implementations thereof, should provide support for software engineering principles such as checking, array bounds checking,[20]: 58–59  detection of attempts to use uninitialized variables, and automatic garbage collection.[20]: 563  Software robustness, durability, and programmer productivity are important.

strong type

The language is intended for use in developing suitable for deployment in distributed environments.

software components

is very important for source code and programmers, especially those already familiar with C and C++.

Portability

Support for [20]: 314  is very important.

internationalization

C# is intended to be suitable for writing applications for both hosted and , ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions.

embedded systems

Although C# applications are intended to be economical with regard to memory and requirements, the language was not intended to compete directly on performance and size with C or assembly language.[21]

processing power

The Ecma standard lists these design goals for C#:[17]

Semicolons are used to denote the end of a statement.

are used to group statements. Statements are commonly grouped into methods (functions), methods into classes, and classes into namespaces.

Curly brackets

Variables are assigned using an , but compared using two consecutive equals signs.

equals sign

are used with arrays, both to declare them and to get a value at a given index in one of them.

Square brackets

The core syntax of the C# language is similar to that of other C-style languages such as C, C++ and Java, particularly:

is supported through .NET APIs, which enable scenarios such as type metadata inspection and dynamic method invocation.

Reflection

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 some homoiconicity to the language.

[72]

are metadata that can be attached to types, members, or entire assemblies, equivalent to annotations in Java. Attributes are accessible both to the compiler and to code through reflection. Many of native attributes duplicate the functionality of GCC's and VisualC++'s platform-dependent preprocessor directives.

Attributes

System.Reflection.Emit namespace, which contains classes that emit metadata and CIL (types, assemblies, etc.) at runtime.

[73]

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.[74]

The .NET Compiler Platform (Roslyn)

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.

[75]

Libraries[edit]

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 (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.[106]


C# can make calls to any library included in the List of .NET libraries and frameworks.

Examples[edit]

Hello World[edit]

The following is a very simple C# program, a version of the classic "Hello world" example using the top-level statements feature introduced in C# 9:[107]

Standardization and licensing[edit]

In August 2001, Microsoft, Hewlett-Packard and Intel co-sponsored the submission of specifications for C# as well as the Common Language Infrastructure (CLI) to the standards organization Ecma International. In December 2001, ECMA released ECMA-334 C# Language Specification. C# became an ISO/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/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.[110] Microsoft has also agreed not to enforce patents relating to Novell products against Novell's paying customers[111] with the exception of a list of products that do not explicitly mention C#, .NET or Novell's implementation of .NET (The Mono Project).[112] However, Novell maintained that Mono does not infringe any Microsoft patents.[113] 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.[114]


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.

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.

Mono

The 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.

Elements

The 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).

DotGNU

Microsoft is leading the development of the open-source reference C# compilers and set of tools. The first compiler, Roslyn, compiles into intermediate language (IL), and the second one, RyuJIT,[115] 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.[116] RyuJIT is open source and written in C++.[117] 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.[4][118] 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.[119] At .NET Framework 4.6, a new JIT compiler replaced the former.[115][120]


Other C# compilers (some of which include an implementation of the Common Language Infrastructure and .NET class libraries):


The Unity game engine uses C# as its primary scripting language. The Godot game engine has implemented an optional C# module thanks to a donation of $24,000 from Microsoft.[121]

Albahari, Joseph (2022). C# 10 in a Nutshell (First ed.). O'Reilly.  978-1-098-12195-2.

ISBN

Archer, Tom (2001). "Part 2, Chapter 4: The Type System". Inside C#. Redmond, Washington: Microsoft Press.  0-7356-1288-9.

ISBN

Novák, István; Velvart, Andras; Granicz, Adam; Balássy, György; Hajdrik, Attila; Sellers, Mitchel; Hillar, Gastón C.; Molnár, Ágnes; Kanjilal, Joydip (2010). Visual Studio 2010 and .NET 4 Six-in-One. Wrox Press.  978-0470499481.

ISBN

Skeet, Jon (2019). C# in Depth (Fourth ed.). Manning.  978-1617294532.

ISBN

Drayton, Peter; Albahari, Ben; Neward, Ted (2002). . O'Reilly. ISBN 0-596-00429-X.

C# Language Pocket Reference

Petzold, Charles (2002). . Microsoft Press. ISBN 0-7356-1370-2.

Programming Microsoft Windows with C#

C# Language Specification

C# Programming Guide

ISO C# Language Specification

C# Compiler Platform ("Roslyn") source code