C Sharp
|
- This article is about the C# programming language. For information about the musical note C#, see musical notation.
C# (pronounced see-sharp) 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 to balance power (the C++ influence) with rapid development (the Visual Basic, Delphi, and Java influences).
Contents |
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 and unchecked arithmetic 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.
- Objects cannot be explicitly freed, but instead are garbage collected when no more references to them exist (although deallocation can be encouraged by destroying references). This prevents dangling references.
- Destructors (see object lifetime) are not available. The closest equivalent is the Disposable interface, which, together with the using block construct, can enforce that an object's resources are promptly deallocated. Finalizers are also provided, but as in Java, these are not prompt.
- As in Java, 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, but C# 2.0 has generics which are more powerful.
- Properties are available, which enable methods to be called using syntax that resembles data member access.
- Full reflection is available.
Although C# is often considered similar to Java, there are also a number of notable differences with this language as well, including the following:
- Java does not have properties or operator overloading
- Java does not have an unsafe mode permitting native pointer manipulation and unchecked arithmetic.
- Java has checked exceptions, while C# exceptions are unchecked, as in C++.
- C# has a goto control flow construct not found in Java.
- Java uses Javadoc-syntax comments to automatically generate documentation from source files. C# uses XML-based comments for this purpose.
- C# supports indexers and delegates.
- C# supports structures in addition to classes. Structures, known in the .NET Framework as value types, are comparable to C structures, in that they need not be heap-allocated and can limit the number of dereferences needed to access data; see value type.
C# 2.0 new language features
New features slated to be included in the upcoming C# 2.0 are:
- Partial classes (separation of class implementation into more than one file)
- Generics or parametrized types. They differ from Java in that parametrized types are first-class citizens in the Virtual Machine, which allows for optimizations and preservation of the type information.
- A new form of iterator that employs coroutines via a functional-style 'yield' keyword similar to 'yield' in Ruby and Python
- Anonymous methods providing closure functionality
Anders Hejlsberg, C#'s creator, discusses the difference between generics as implemented in C# and other languages in this interview (http://www.artima.com/intv/generics2.html).
Code libraries
Unlike most other programming languages, no C# implementation currently includes any distinct set of class or function libraries. Instead, C# is tied closely to the .NET Framework, from which C# obtains its runtime functions and classes. 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
Here is a very simple C# program:
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. Let's break down each element:
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()
. Don't worry about the public static void part for now.
System.Console.WriteLine("Hello world!");
Here's where the action happens. Console is a system object, representing a command-line console where a program can input and output text. We are calling 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. Due 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 various debates. 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 C# up 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.
See also
- Mono, an open source implementation of C# and .NET
- F# programming language
- Cω programming language, extension to C#
- Nemerle programming language
- Boo programming language, a cross between C# and Python
- Polyphonic C#
- Comparison of C# to Java
- Common Language Runtime
- Anders Hejlsberg
Books
- Computer-Books.us (http://www.computer-books.us/csharp.php) - Online C# Books
External links
- C# Language (MSDN) (http://msdn.microsoft.com/vcsharp/team/language/default.aspx)
- Hyperlinked ECMA C# Language Specification (http://www.jaggersoft.com/csharp_standard/toc.htm)
- ECMA-334 C# Language Specification (.pdf) (http://www.ecma-international.org/publications/files/ecma-st/ECMA-334.pdf)
- ISO/IEC 23270:2003 C# Language Specification (http://www.iso.org/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=36768&ICS1=35&ICS2=60&ICS3=)
- Microsoft Visual C# .NET (http://msdn.microsoft.com/vcsharp/)
- C# SQL Server and Mobile Device Developer Information by Sarah Blow (http://geekswithblogs.net/waterbaby/)
- MCS: The Mono C# compiler (http://www.mono-project.com/using/mcs.html)
- Portable.NET (http://www.southern-storm.com.au/portable_net.html)
- DotGNU Project - a Free Software .NET compatible platform (http://www.dotgnu.org/)
- Borland's C# Builder for the Microsoft® .NET Framework (http://www.borland.com/csharpbuilder/)
- Borland's Delphi 2005 IDE (C#Builder, Delphi Win32 + Delphi .NET in one IDE) (http://www.borland.com/delphi/)
- C# tutorial including MSIL by Aleksey Nudelman (http://csharpcomputing.com/Tutorials/TOC.htm)
- SharpDevelop: Open Source C# IDE (http://www.icsharpcode.net)
- Free C# Tutorial by Joe Mayo (http://www.csharp-station.com/Tutorial.aspx)
- C# Home (http://www.csharp-home.com/) C# tutorials, code and contests
- C# Friends (http://www.csharpfriends.com/)
- C# Help (http://www.csharphelp.com/)
- C# Station (http://www.csharp-station.com/) tutorials and articles on C#
- CodeProject C# section (http://www.codeproject.com/csharp/) articles on C# with source code
- Developer Fusion C# Section (http://www.developerfusion.co.uk/csharp/) tutorials, articles and source code
- DeveloperLand C# resource portal (http://www.developerland.com/) tutorials , book reviews and articles on C# with source code
- Doing Objects in VB.NET and C# (http://terrysmith.net/software/dotnet_ebook/index.html) a free eBook
- news://msnews.microsoft.com/microsoft.public.dotnet.languages.csharp
Template:Major programming languages smallbg:C Sharp cs:Csharp da:C Sharp de:C-Sharp es:C Sharp eo:C dieso (programlingvo) fr:C sharp it:C sharp he:C Sharp lt:C sharp 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:ภาษาซีชาร์ป tr:C Sharp programlama dili uk:C Sharp zh:C#