Advertisement

C Sharp

From Academic Kids

Template:Title

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:

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

Template:Wikibooks

Books

External links

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#

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools