C Sharp (programming language)
#
Paradigm | structured, imperative |
---|---|
Designed by | Microsoft Corporation |
First appeared | 2001 (last revised 2005) |
Typing discipline | static, strong, both safe and unsafe, nominative |
Website | docs |
Major implementations | |
.NET Framework, Mono | |
Dialects | |
1.0, 1.5, 2.0 (ECMA) | |
Influenced by | |
Java programming language, C++ | |
Influenced | |
Nemerle |
C# (see section on naming, pronunciation) is an object-oriented programming language developed by Microsoft as part of their .NET initiative, and later approved as a standard by ECMA and ISO. C# has a procedural, object oriented syntax based on C++ that includes aspects of several other programming languages (most notably Delphi, Visual Basic, and Java) with a particular emphasis on simplification (fewer symbolic requirements than C++, fewer decorative requirements than Java).
Architectural history
C#'s principal designer, and lead architect at Microsoft, was Anders Hejlsberg. His previous experience in programming language and framework design (Visual J++, Borland Delphi, Turbo Pascal) can be readily seen in the syntax of the C# language, as well as throughout the CLR (Common Language Runtime) core. He can be cited in interviews and technical papers as stating flaws in most major programming languages, for example, C++, Java, Delphi, Smalltalk, were what drove the fundamentals of the CLR, which, in turn, drove the design of the C# programming language itself. His expertise can be seen in C#. There is a critical argument that C# shares roots in other languages, as purported by programming language history chart. C# was designed to fit both demands for a concise syntax (C++) and 'unlimited' rapid development (versus the 'limited' RAD of Visual Basic).
Language features
C# is, in some senses, the programming language which most directly reflects the underlying Common Language Infrastructure (CLI). It depends strongly on this framework because it was designed specifically to take advantage of the features that the CLI provides. Most of C#'s intrinsic types correspond to value-types implemented by the CLI framework. A common misbelief is that they are garbage-collected, though they are not; they are true value-types and are stack allocated (with an exception for System.Object, and due to interning, System.String).
Applications written in C# require an implementation of the Common Language Runtime (CLR) to execute, in the same way that VB6 requires a runtime to execute (this is often confused with the JRE, which provides a byte-code interpreter). Unlike Java classes, CLI applications are compiled in 2 passes. First compiled to platform abstract bytecode, and secondly, it's compiled at first runtime of the application to the native client's machine code.
Compared to C and C++, the language is restricted or enhanced in a number of ways, including but not limited to the following:
- True support for pointers. However pointers can only be used within unsafe scopes, and only programs with appropriate permissions can execute code marked as unsafe. Most object access is done through safe references, which cannot be made invalid, and most arithmetic is checked for overflow. An unsafe pointer can be made to not only value-types, but to subclasses of System.Object as well. Also safe code can be written that uses a pointer (System.IntPtr).
- Managed memory cannot be explicitly freed, but instead is garbage collected when no more references to the memory exist. (Objects that reference unmanaged resources, such as an HBRUSH, can be instructed to release those resources through the standard
IDisposable
interface, which provides a pattern for deterministic deallocation of resources.) - Multiple inheritance is prohibited (although a class can implement any number of interfaces). This was a design decision by the language's lead architect (Anders Hejlsberg) to avoid complication, avoid 'dependency hell,' and simplify architectural requirements throughout CLI.
- C# is more typesafe than C++. The only implicit conversions by default are safe conversions, such as widening of integers and conversion from a derived type to a base type (and this is enforced at compile-time and, indirectly, during JIT). There are no implicit conversions between booleans and integers and between enumeration members and integers, and any user-defined implicit conversion must be explicitly marked as such, unlike C++'s copy constructors.
- Syntax for array declaration is different ("
int[] a = new int[5];
" instead of "int a[5];
"). - Enumeration members are placed in their own namespace.
- C# 1.0 lacks templates, however, C# 2.0 provides generics.
- Properties are available which results in syntax that resembles C++ member field access, similar to VB.
- Full type reflection and discovery is available.
C# 2.0 new language features
New features in C# for the .NET SDK 2.0 (corresponding to the 3rd edition of the ECMA ECMA-334 standard) are:
- Partial classes (separation of class implementation into more than one file)
- Generics or parameterized types. They support some features not supported by C++ templates such as type constraints on generic parameters. On the other hand, expressions cannot be used as generic parameters, as with C++ templates. Also, they differ from Java in that parameterized types are first-class objects in the Virtual Machine, which allows for optimizations and preservation of the type information. See a simple example of C# 2.0 generics.
- Static classes which represent a concept close to VB.NET modules, cannot be instantiated from code and allow only static members.
- A new form of iterator that employs coroutines via a functional-style
yield
keyword similar toyield
in Python. - Anonymous delegates providing closure functionality.
- Covariance and contravariance for signatures of delegates
- Coalesce operator: (??) returns the first non-null value in a list:
object nullObj = null; object obj = new Object(); return nullObj ?? obj; //returns obj
The primary use of this operator is to assign a nullable type to a non-nullable type with an easy syntax:
int? i = null; int j = i ?? default(int); //can't assign null to int
- Nullable value types (denoted by a question mark, ie
int? i = null;
), allowing improved interaction with SQL databases.
Nullable types received an eleventh hour improvement at the end of August 2005 (only weeks before the official launch), to improve their boxing characteristics: a nullable variable which is assigned null is not actually a null reference (it's a value type). Hence boxing this value would result in a non-null reference. The following code illustrates the flaw:
int? i = null; object o = i; if (o == null) Console.WriteLine("Correct behaviour - you are running a version from Sept 05 or later"); else Console.WriteLine("Incorrect behaviour, prior to Sept 05 releases");
The late nature of this fix caused some controversy, since it required core-CLR changes affecting not only .NET2, but all dependent technologies (including C#, VB, SQL Server 2005 and Visual Studio 2005).
C# 3.0 new language features
In C# 3.0 there will be radical additions:
- "
select
,from
,where
" keywords allowing to query from SQL, XML, collections, and more (Language integrated query (LINQ)) - Object initialization :
Customer c = new Customer(); c.Name="James";
becomesCustomer c = new Customer { Name="James" };
- Lambda expressions :
listOfFoo.Where(delegate(Foo x) { return x.size>10;})
becomeslistOfFoo.Where(x => x.size>10);
- Local variable type inference:
var x = "hello";
is interchangeable withstring x = "hello";
- Anonymous types :
var x = new { Name = "James" }
- Extension methods (adding methods to classes by including the
this
keyword in the first parameter)
C# 3.0 was unveiled at the PDC 2005, and a Preview, with specifications is available From the MSDN Page (MSDN).
Language researchers at Microsoft have emphasized that C# 3.0 is bytecode-compatible with C# 2.0 — essentially the improvements are purely syntactic or compile-time improvements. For example, many of the most common integrated queries can already be implemented using anonymous delegates in combination with predicate-based container methods such as List.FindAll
and List.RemoveAll
.
Code libraries
The ECMA C# specification details a minimum set of types and class libraries that the compiler expects to have available and they define the basics required. Most implementations in the open ship with the larger set of libraries.
The .NET Framework is a class library which can be used from a .NET language to perform tasks from simple data representation and string manipulation to generating dynamic web pages (ASP.NET), XML parsing, Web Services/Remoting (SOAP) and reflection. The code is organized into a set of namespaces which group together classes with a similar function, e.g. System.Drawing
for graphics, System.Collections
for data structures and System.Windows.Forms
for the Windows Forms system.
A further level of organisation is provided by the concept of an assembly. An assembly can be a single file or multiple files linked together (through al.exe) which may contain many namespaces and objects. Programs needing classes to perform a particular function might reference assemblies such as System.Drawing.dll and System.Windows.Forms.dll as well as the core library (known as mscorlib.dll in Microsoft's implementation).
Hello world example
The following is a very simple C# program, a version of the classic "Hello world" example.
public class ExampleClass { public static void Main() { System.Console.WriteLine("Hello, world!"); } }
The effect is to write the text Hello, world! to the output console. Each line serves a specific purpose, as follows:
public class ExampleClass
This is a class definition. It is public, meaning objects in other projects can freely use this class. All the information between the following braces describes this class.
public static void Main()
This is the entry point where the program begins execution. It could be called from other code using the syntax ExampleClass.Main()
. (The public static void portion is a subject for a slightly more advanced discussion.)
System.Console.WriteLine("Hello, world!");
This line performs the actual task of writing the output. Console is a system object, representing a command-line console where a program can input and output text. The program calls the Console method WriteLine, which causes the string passed to it to be displayed on the console.
Standardization
In August, 2000, Microsoft Corporation, Hewlett-Packard and Intel Corporation co-sponsored the submission of specifications for C# as well as the Common Language Infrastructure (CLI) the international standardization organization ECMA. In December 2001, ECMA released ECMA-334 C# Language Specification. C# became an ISO standard in 2003 (ISO/IEC 23270). 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 (similar to C++ templates). In July 2005, ECMA submitted the standards and related TRs to ISO/IEC JTC 1 via the latter's Fast-Track process. This process usually takes 6-9 months.
Based on these standards, there are independent implementations being worked on, including:
- Mono, Novell's open source .NET implementation (originally by Ximian).
- dotGNU, and Portable.NET from the Southern Storm Software, PTY
Microsoft released support of the 3rd edition of C# in the .NET SDK 2.0, and Visual Studio 2005, in November 2005.
Politics
Many of Microsoft's products and initiatives generate political attention, and C# is no exception. Owing to C#'s close relationship with a commercial institution, political discussions continue regarding the legitimacy of C# standardization, its Java similarities, its future as a general-purpose language, and other issues. Some security experts express skepticism as to the efficacy of the CLR's security mechanisms, and criticise their complexity. At the same time, the language is praised for its clear and programmer-friendly grammar, in addition to reduction in development time for certain types of applications.
Unlike proprietary languages such as Visual Basic, Microsoft chose to open up C# to the standardization process. However, Microsoft is still a primary force driving changes and innovation in the language. Additionally, Microsoft has made it clear that C#, as well as the other .NET languages, is an important part of its software strategy for both internal use and external consumption. Microsoft takes an active role in marketing the language as part of its overall business strategies.
Language name
According to the ECMA-334 C# Language Specification, section 6, Acronyms and abbreviations [1] the name of the language is written "C#" ("LATIN CAPITAL LETTER C (U+0043) followed by the NUMBER SIGN # (U+0023)") and pronounced "C Sharp".

Due to technical limitations of display (fonts, browsers, etc.) and the fact that the sharp symbol (♯, U+266F, MUSIC SHARP SIGN, see graphic at right if the symbol is not visible) is not present on the standard keyboard, the number sign (#) was chosen to represent the sharp symbol in the written name of the language. So, although the symbol in "C#" represents the sharp symbol, it is actually the number sign ("#"). Although Microsoft's C# FAQ refers to the sharp symbol in the language name, Microsoft clarifies the language name as follows:
"The spoken name of the language is "C sharp" in reference to the musical "sharp" sign, which increases a tone denoted by a letter (between A and G) by half a tone. However, for ease of typing it was decided to represent the sharp sign by a pound symbol (which is on any keyboard) rather than the "musically correct" Unicode sharp sign. The Microsoft and ECMA 334 representation symbols thus agree: the # in C# is the pound sign, but it represents a sharp sign. Think of it in the same way as the <= glyph in C languages which is a less than sign and an equals sign, but represents a less-than-or-equals sign.", Microsoft Online Customer Service
The choice to represent the sharp symbol (♯) with the number sign (#) has led to confusion regarding the name of the language. For example, although most printed literature uses the correct number sign [2], some incorrectly uses the sharp symbol. What's more, users have been known to call the language "see-pound" (in the US the #-key on telephones is pronounced as the "pound"-key) or "see-hash". Also in the US the # symbol is also occasionally referred to as the "gate" symbol on a telephone, leading to a pronunciation of the language as "see-gate", which could be confused with the brand name of hard-drive manufacturer, Seagate.
The "sharp" suffix has been emulated by a number of other .NET languages that are variants of existing languages, including J# (Microsoft's implementation of Java), A# (from Ada), F# (presumably from System F, the type system used by the ML family), and Gtk# (a .NET wrapper for GTK+).
The # symbol also makes a visual programmers joke. The ++ (increment) operator signifies an increase, or improvement. Hence C++ is an improvement over C. C# can be seen as ++ stacked on top of ++, therefore even a further increase.
See also
- SharpDevelop, an open-source C# IDE for Windows
- DotGNU, an open source implementation of .NET
- Mono, an open source implementation of .NET
- MonoDevelop, an open-source C# IDE for Linux
- F# programming language
- Cω programming language, extension to C#
- D programming language
- Spec♯
- Sing♯
- Nemerle programming language
- Boo programming language, a cross between C# and Python
- IronPython, a Microsoft-supported, .NET-compliant version of Python
- Polyphonic C#
- Comparison of C# and Java
- Common Language Runtime
- Anders Hejlsberg
- C++/CLI
- Comparison of programming languages
External links
- C# Language (MSDN)
- C# C# Open Source
- C# Online.NET
- C# Friends
- C# Specification
- ECMA-334 C# Language Specification (.pdf)
- ISO C# Language Specification (for purchase)
- Microsoft Visual C# .NET
- MCS: The Mono C# compiler
- Portable.NET
- Borland's C# Builder for the Microsoft® .NET Framework
- SharpDevelop: Open Source C# IDE
- Microsoft Visual C# Express Edition, which is downloadable for free
- news://msnews.microsoft.com/microsoft.public.dotnet.languages.csharp
- Ruby.net: Compiler for the Ruby language that targets the .NET CLR.
- Anders Hejlsberg, C#'s creator, discusses the difference between the implementations of generics in C#, Java, and C++ in this interview.