C Sharp
- The correct title of this article is C#. The substitution or omission of a # sign is due to technical restrictions.
- This article is about the programming language. For the musical note, see musical notation.
| Paradigm: | structured, imperative |
|---|---|
| Appeared in: | 2001 (last revised 2005) |
| Designed by: | Microsoft Corporation |
| Typing discipline: | dynamic, strong, unsafe, nominative |
| Major implementations: | .NET Framework, Mono |
| Dialects: | Microsoft Visual Studio .NET, .NET 2003, 2005 |
| Influenced by: | Java programming language, [[C++]] |
| Influenced: | None |
C# (see section on naming, pronunciation) is an object-oriented programming language developed by Microsoft as part of their .NET initiative. 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).
Contents |
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, stating that these flaws were what drove the fundamentals of the CLR, which is what 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 Runtime (CLR) on which all .NET programs run, and it depends strongly on this framework because it was designed specifically to take advantage of the features that the CLR provides. Most of C#'s intrinsic types all correspond to value-types implemented by the .NET 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 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 .NET programs are 2-pass compiled, stored as first-pass binary code and second-pass compiled at the client workstation. Java programs require a Java Virtual Machine. VB6 Programs require a support library, likewise, .NET programs require a set of support libraries and a core execution environment (which handles the initialization and initial JIT process).
Compared to C and C++, the language is restricted or enhanced in a number of ways, including but not limited to the following:
- Pointers can only be within an unsafe scope, 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. Pointers can only be made to so-called value types; objects managed by the garbage collector can only be referred to. 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) however no direct manipulation of the memory at the stored address can be performed from C# without the unsafe and fixed keywords, unless calls to Win32 API functions such as rtlMoveMemory are made.
- 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
IDisposableinterface, which provides a pattern for deterministic deallocation of resources.) - Only single inheritance is available, but 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 the CLR.
- 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, between enumeration members and integers, no void pointers (although references to an 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# 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# 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 objects in the Virtual Machine, which allows for optimizations and preservation of the type information. See a simple example of C# 2.0 generics. Anders Hejlsberg, C#'s creator, discusses the difference between the implementations of generics in C#, Java, and C++ in this interview.
- A new form of iterator that employs coroutines via a functional-style
yieldkeyword similar toyieldin Python. - Anonymous methods providing closure functionality.
- Coalesce operator: (??) returns the first non-null value in a list:
object nullObj = null; object obj = new Object(); return nullObj ?? obj // returns obj;
- 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
thiskeyword 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 release 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 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".
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.
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+]]).
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]]
External links
- C# Language (MSDN)
- 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
| Major programming languages (more) (edit) | |||
|
Industrial: ABAP | Ada | AWK | Assembly | ColdFusion | C | [[C++]] | C# | COBOL | Delphi | Eiffel | Fortran | Java | JavaScript | Limbo | Lua | Objective-C | Pascal | Perl | PHP | Python | RPG | Scheme | Smalltalk | SQL | Tcl | Visual Basic | VB.NET | Visual FoxPro
|
ca:C sostingut cs:Csharp da:C Sharp de:C-Sharp es:C Sharp eo:C dieso (programlingvo) eu:C Sharp fa:سیشارپ fr:C sharp ko:C 샤프 id:C sharp it:C sharp he:C sharp lt:C sharp hu:C Sharp programozási nyelv nl:C sharp ja:C Sharp no:C Sharp pl:C Sharp pt:C sharp ru:C Sharp sk:C Sharp (programovací jazyk) sr:C Sharp fi:C sharp sv:C-sharp th:ภาษาซีชาร์ป vi:C thăng tr:C Sharp programlama dili uk:C Sharp (мова програмування) zh:C#
Categories: Articles with a number sign in their title | .NET programming languages | C programming language family | Class-based programming languages | Statically-typed programming languages | Concurrent programming languages | Curly bracket programming languages | IEC standards | ISO standards | Programming languages