- This article is about the C# programming language. For information about the musical note C#, see musical notation.
C# (see section on naming, pronunciation) is an object-oriented programming language developed by Microsoft as part of their .NET initiative. Microsoft based C# on C++ and Java. C# was designed as a language that would provide syntax that would be comfortable for C++ and Java programmers, with rapid development functionality that would suit Visual Basic and Delphi programmers.
Language features
C# is, in some sense, the programming language which most directly reflects the underlying .NET Framework on which all .NET programs run, and it depends strongly on this framework; there is no such thing as an unmanaged C# program. Its primitive datatypes are objects of the corresponding .NET types, it is garbage-collected, and many of its abstractions, such as its classes, interfaces, delegates, exceptions, and so on, expose explicit features of the .NET runtime.
Compared to C and C++, the language is restricted or enhanced in a number of ways, including but not limited to the following:
- Raw pointers can only be used in a special unsafe mode. Most object access is done through safe references, which cannot be made invalid, and most arithmetic is checked for overflow. Pointers can only be made to so-called value types; objects managed by the garbage collector can only be referred to.
- Objects cannot be explicitly freed, but instead are garbage collected when no more references to them exist. (Objects representing unmanaged resources, though, can be instructed to release those resources through the standard
IDisposable
interface.) - Only single inheritance is available, but a class can implement any number of abstract interfaces. This functions mainly to simplify the runtime's implementation.
- 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. There are no implicit conversions between booleans and integers, between enumeration members and integers, no void pointers (although references to Object are similar), 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# has no templates.
- Properties are available, which enable methods to be called using syntax that resembles data member access.
- Full reflection is available.
C# 2.0 new language features
New features in C# 2.0 are:
- Partial types (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 citizens in the Virtual Machine, which allows for optimizations and preservation of the type information. See a simple example of C# 2.0 generics.
- A new form of iterator that employs coroutines via a functional-style
yield
keyword similar toyield
in Python - Anonymous methods providing closure functionality
- Nullable value types (denoted by a question mark, ie 'int? i = null;', allowing improved interaction with SQL databases
Anders Hejlsberg, C#'s creator, discusses the difference between the implementations of generics in C#, Java, and C++ in this interview.
Nullable types received an 11th 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 collections, sql and more (called LINQ - Language INtegrated Query) - 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";
- 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 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
Microsoft has submitted C# to the ECMA for formal standardization. In December 2001, ECMA released ECMA-334 C# Language Specification. C# became an ISO standard in 2003 (ISO/IEC 23270). There are independent implementations being worked on, including:
- Mono, Novell's open source .NET implementation (originally by Ximian).
- dotGNU, and Portable.NET from the Free Software Foundation
More recently, Microsoft has added support in beta releases of Visual Studio 2005 for generics (similar to C++ templates), partial types and some other new features. ECMA/ISO standardization of these new features has been proposed, but they are not currently part of the standard language definition.
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 dramatic drops in application development time (as opposed to C++).
Unlike proprietary languages such as Visual Basic or Java, 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
The name "C#" may have been chosen by Microsoft to imply progression from the C++ language, with the # symbol resembling two + symbols merged together, or four + symbols arranged in a square.
According to Microsoft [1], the symbol in the name "C#" is ♯, the musical sharp symbol (see graphic at right if the symbol is not visible). Therefore, the proper representation is "C♯". It follows then that the name of the language is pronounced "see-sharp", like the musical note.
However, according to the ECMA-334 C# Language Specification, section 6, Acronyms and abbreviations [2] 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 ♯ sharp symbol is not present on the standard keyboard, the # hash symbol is often used. Unfortunately, the hash symbol is often used in books as well [3], presumably leading to more confusion regarding the name of the language. 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.
Another name for the # symbol is "octothorpe", which when combined with a hard C sound, results in the pronunciation "cock-toe-thorp". This form, spoken emphasising the first syllable, is occasionally used as an expletive by programmers.
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+).
See also
- Mono, an open source implementation of C# and .NET
- 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# to Java
- Common Language Runtime
- Anders Hejlsberg
- C++/CLI
External links
- C# Language (MSDN)
- C# Specification
- ECMA-334 C# Language Specification (.pdf)
- Microsoft Visual C# .NET
- Computer-Books.us - A collection of C# books available for free download.
- C# Community Site
- MCS: The Mono C# compiler
- Portable.NET
- Borland's C# Builder for the Microsoft® .NET Framework
- SharpDevelop: Open Source C# IDE
- news://msnews.microsoft.com/microsoft.public.dotnet.languages.csharp