C# (C–Sharp)
C# is an object-oriented programming language developed by Microsoft as part of their .NET initiative. Microsoft based C# on C++ and was influenced by some aspects of Visual Basic. C# has a procedural, object oriented syntax that combines aspects of several other programming languages (most notably C++, Delphi, Visual Basic, and Java) with a particular emphasis on simplification (fewer symbolic requirements than C++, less decorative requirements than Java.)
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 exists. (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 languages lead architect (Anders Hejlsberg) to avoid complication, avoid 'dependancy 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 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# V1 lacks templates, however, C# V2 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.
