C Sharp Article Index for
C
Website Links For
Sharp
 

Information About

C Sharp




  name C#
  year 2001 (last revised 2005 )
  designer Microsoft Corporation
  implementations NET Framework , Mono
  influenced By Java Programming Language , C++
  typing Dynamic , Strong , Both Safe And Unsafe , Nominative
  dialects 10, 15, 20 (ECMA)
  influenced Nemerle
  paradigm Structured , Imperative


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).


ARCHITECTURAL HISTORY

C#'s principal designer, and lead architect at Microsoft, was (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), and 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 all 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# 2.0 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 .

  • A new form of Iterator that employs Coroutine s via a functional-style yield keyword similar to yield in Python .

  • Anonymous Delegate s 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"; becomes Customer c = new Customer { Name="James" };

  • Lambda Expression s : listOfFoo.Where(delegate(Foo x) { return x.size>10;}) becomes listOfFoo.Where(x => x.size>10);

  • Local variable type inference: var x = "hello"; is interchangeable with string 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 Page s (ASP.NET), XML parsing, Web Services/Remoting ( SOAP ) and Reflection . The code is organized into a set of Namespace s 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:

More recently, Microsoft has added support in release of Visual Studio 2005 for Generics (similar to C++ Templates ), partial classes and some other new features.


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'' {Link without Title} 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 {Link without Title} , 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



EXTERNAL LINKS