C Sharp (programming language): Difference between revisions
→XML documentation system: Added multi-line comment example |
→XML documentation system: Added reference |
||
Line 225: | Line 225: | ||
</source> |
</source> |
||
Multi-line comments, while defined in the version 1.0 language specification, were not supported until the [[.NET Framework|.NET]] 1.1 release. These comments are designated by <code>/**<code>. |
Multi-line comments, while defined in the version 1.0 language specification, were not supported until the [[.NET Framework|.NET]] 1.1 release<ref>{{cite web |url=http://blogs.msdn.com/ansonh/archive/2006/09/11/750056.aspx |title=C# XML documentation comments FAQ |accessdate=2007-12-11 |author=Anson Horton |date=2007-09-11 }}</ref>. These comments are designated by <code>/**<code>. |
||
<source lang="csharp"> |
<source lang="csharp"> |
Revision as of 05:44, 12 December 2007
#
Paradigm | structured, imperative, object-oriented |
---|---|
Designed by | Microsoft Corporation |
First appeared | 2001 (last revised 2007) |
Typing discipline | static, strong, both safe and unsafe, nominative |
Website | docs |
Major implementations | |
.NET Framework, Mono, DotGNU | |
Dialects | |
1.0, 1.5 , 2.0 (ECMA), 3.0 | |
Influenced by | |
Delphi, C++, Java, Modula-3, Eiffel | |
Influenced | |
Nemerle, D, Java[1] |
C# (see section on name, pronunciation) is an object-oriented programming language developed by Microsoft as part of the .NET initiative and later approved as a standard by ECMA and ISO. Anders Hejlsberg leads development of the C# language, which has a procedural, object-oriented syntax based on C++ and includes aspects of several other programming languages (most notably Delphi and Java) with a particular emphasis on simplification.
This article describes the C# language as defined in the ECMA and ISO standards. For a description of Microsoft's implementation, see Microsoft Visual C#.
Design goals
The ECMA standard lists these design goals for C#:
- C# is intended to be a simple, modern, general-purpose, object-oriented programming language.
- Because software robustness, durability and programmer productivity are important, the language should include strong type checking, array bounds checking, detection of attempts to use uninitialized variables, source code portability, and automatic garbage collection.
- The language is intended for use in developing software components that can take advantage of distributed environments.
- Programmer portability is very important, especially for those programmers already familiar with C and C++.
- Support for internationalization is very important.
- C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions.
- Although C# applications are intended to be economical with regards to memory and processing power requirements, the language was not intended to compete directly on performance and size with C or assembly language.
Architectural history
C#'s principal designer and lead architect at Microsoft is 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 Common Language Runtime (CLR) core. In interviews and technical papers he has stated that flaws in most major programming languages (e.g. C++, Java, Delphi, and Smalltalk) drove the fundamentals of the CLR, which, in turn, drove the design of the C# programming language itself. Some argue that C# shares roots in other languages.[2]
Features
The following description is based on the language standard and other documents listed in the External Reference section.
By design, C# is the programming language that most directly reflects the underlying Common Language Infrastructure (CLI). Most of C#'s intrinsic types correspond to value-types implemented by the CLI framework. However, the C# language specification does not state the code generation requirements of the compiler: that is, it does not state that a C# compiler must target a Common Language Runtime (CLR), or generate Common Intermediate Language (CIL), or generate any other specific format. Theoretically, a C# compiler could generate machine code like traditional compilers of C++ or FORTRAN; in practice, all existing C# implementations target CLI.
C# differs from C and C++ in many ways, including:
- There are no global variables or functions. All methods and members must be declared within classes.
- Local variables cannot shadow variables of the enclosing block, unlike C and C++. Variable shadowing is often considered confusing by C++ texts.
- C# supports a strict boolean type,
bool
. Statements that take conditions, such aswhile
andif
, require an expression of a boolean type. While C++ also has a boolean type, it can be freely converted to and from integers, and expressions such asif(a)
require only thata
is convertible to bool, allowinga
to be an int, or a pointer. C# disallows this 'integer meaning true or false' approach on the grounds that forcing programmers to use expressions that return exactlybool
prevents certain types of programming mistakes. - In C#, pointers can only be used within blocks specifically marked as unsafe, and programs with unsafe code need appropriate permissions to run. Most object access is done through safe references, which cannot be made invalid. An unsafe pointer can point to an instance of a value-type, array, string, or a block of memory allocated on a stack. Code that is not marked as unsafe can still store and manipulate pointers through the
System.IntPtr
type, but cannot dereference them. - Managed memory cannot be explicitly freed, but is automatically garbage collected. Garbage collection addresses memory leaks. C# also provides direct support for deterministic finalization with the
using
statement (supporting the Resource Acquisition Is Initialization idiom). - Multiple inheritance is not supported, although a class can implement any number of interfaces. This was a design decision by the language's lead architect 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. This is enforced at compile-time, during JIT, and, in some cases, at runtime. There are no implicit conversions between booleans and integers and between enumeration members and integers (except 0, which can be implicitly converted to an enumerated type), and any user-defined conversion must be explicitly marked as explicit or implicit, unlike C++ copy constructors (which are implicit by default) and conversion operators (which are always implicit).
- Enumeration members are placed in their own namespace.
- Accessors called properties can be used to modify an object with syntax that resembles C++ member field access. In C++, declaring a member public enables both reading and writing to that member, and accessor methods must be used if more fine-grained control is needed. In C#, properties allow control over member access and data validation.
- Full type reflection and discovery is available.
Unified type system
C# has a unified type system. This means that all types, including primitives such as integers, are subclasses of the System.Object
class. For example, every type inherits a ToString()
method. For performance reasons, primitive types (and value types in general) are internally allocated on the stack. Boxing and unboxing allow one to translate primitive data to and from their object form. Effectively, this makes the primitive types a subtype of the Object type. Primitive types can also define methods (e.g., 42.ToString()
calls the ToString()
method on an integer), and in the programmer's perspective behave like any other object.
C# allows the programmer to create user-defined value types, using the struct
keyword. From the programmer's perspective, they can be seen as lightweight classes. Unlike regular classes, and like the standard primitives, such value types are allocated on the stack rather than on the heap. They can also be part of an object (either as a field or boxed), or stored in an array, without the memory indirection that normally exists for class types. Structs also come with a number of limitations. Because structs have no notion of a null value and can be used in arrays without initialization, they are implicitly initialized to default values (normally by filling the struct memory space with zeroes, but the programmer can specify explicit default values to override this). The programmer can define additional constructors with one or more arguments. This also means that structs lack a virtual method table, and because of that (and the fixed memory footprint), they cannot allow inheritance (but can implement interfaces).
C# 2.0 new language features
New features in C# for the .NET SDK 2.0 (corresponding to the 3rd edition of the ECMA-334 standard) are:
- Partial classes allow class implementation across more than one file. This permits breaking down very large classes, or is useful if some parts of a class are automatically generated.
file1.cs:
public partial class MyClass
{
public MyClass()
{
// implementation
}
}
file2.cs:
public partial class MyClass
{
public void SomeMethod()
{
// implementation
}
}
- Generics or parameterized types. This is a .NET 2.0 feature supported by C#. Unlike C++ templates, .NET parameterized types are instantiated at runtime rather than by the compiler; hence they can be cross-language whereas C++ templates cannot. They support some features not supported directly by C++ templates such as type constraints on generic parameters by use of interfaces. On the other hand, C# does not support non-type generic parameters. Unlike generics in Java, .NET generics use reification to make parameterized types first-class objects in the CLI Virtual Machine, which allows for optimizations and preservation of the type information.
- Static classes that cannot be instantiated, and that only allow static members. This is similar to the concept of module in many procedural languages.
- A new form of iterator that provides generator functionality, using a
yield return
construct similar toyield
in Python.
// Method that takes an iterable input (possibly an array) and returns all even numbers.
public static IEnumerable<int> GetEven(IEnumerable<int> numbers)
{
foreach (int i in numbers)
{
if (i % 2 == 0) yield return i;
}
}
- Anonymous delegates providing closure functionality.
public void Foo(object parameter) {
// ...
ThreadPool.QueueUserWorkItem(delegate
{
// anonymous delegates have full access to local variables of the enclosing method
if (parameter == ...)
{
// ...
}
// ...
});
}
- Covariance and contravariance for signatures of delegates
- The accessibility of property accessors can be set independently. Example:
string status = string.Empty;
public string Status
{
get { return status; } // anyone can get value of this property,
protected set { status = value; } // but only derived classes can change it
}
- Nullable value types (denoted by a question mark, e.g.
int? i = null;
) which addnull
to the set of allowed values for any value type. This provides improved interaction with SQL databases, which can have nullable columns of types corresponding to C# primitive types: an SQLINTEGER NULL
column type directly translates to the C#int?
.
Nullable types received an eleventh-hour improvement at the end of August 2005, mere weeks before the official launch, to improve their boxing characteristics: a nullable variable which is assigned null is not actually a null reference, but rather an instance of struct Nullable<T>
with property HasValue
equal to false
. When boxed, the Nullable
instance itself is boxed, and not the value stored in it, so the resulting reference would always be non-null, even for null values. The following code illustrates the corrected flaw:
int? i = null;
object o = i;
if (o == null)
Console.WriteLine("Correct behaviour - runtime version from September 2005 or later");
else
Console.WriteLine("Incorrect behaviour - pre-release runtime (from before September 2005)");
When copied into objects, the official release boxes values from Nullable
instances, so null values and null references are considered equal. The late nature of this fix caused some controversy[citation needed], since it required core-CLR changes affecting not only .NET2, but all dependent technologies (including C#, VB, SQL Server 2005 and Visual Studio 2005).
- Coalesce operator: (
??
) returns the first of its operands which is not null:
object nullObj = null;
object obj = new Object();
return nullObj ?? obj; // returns obj
The primary use of this operator is to assign a nullable type to a non-nullable type with an easy syntax:
int? i = null;
int j = i ?? 0; // Unless i is null, initialize j to i. Else (if i is null), initialize j to 0.
C# 3.0 new language features
C# 3.0 is the current version being released on Monday the 19th of November 2007. It includes new features inspired by functional programming languages such as Haskell and ML, and is driven largely by the introduction of the Language Integrated Query (LINQ) pattern to the Common Language Runtime.[3]
- Language Integrated Query:[4] "
from
,where
,select
" context-sensitive keywords allowing queries across SQL, XML, collections, and more. These are treated as keywords in the LINQ context, but their addition won't break existing variables namedfrom
,where
, orselect
. - Object initializers:
Customer c = new Customer(); c.Name = "James";
can be writtenCustomer c = new Customer { Name="James" };
- Collection initializers:
MyList list = new MyList(); list.Add(1); list.Add(2);
can be written asMyList list = new MyList { 1, 2 };
(assuming that MyList implementsSystem.Collections.IEnumerable
and has a public Add method[5]) - Anonymous types:
var x = new { Name = "James" }
- Local variable type inference:
var x = "hello";
is interchangeable withstring x = "hello";
. More than just syntactic sugar, this feature is required for the declaration of anonymous typed variables. - Implicitly-typed arrays: The type of an array can now be omitted, so that
int[] arr = new int[] { 1, 2, 3 }
can now be written asvar arr = new[] { 1, 2, 3 }
. - Lambda expressions:
listOfFoo.Where(delegate(Foo x) { return x.size > 10; })
can be writtenlistOfFoo.Where(x => x.size > 10);
- Compiler-inferred translation of Lambda expressions to either strongly-typed function delegates or strongly-typed expression trees
- Automatic properties: The compiler will automatically generate a private instance variable and the appropriate getter and setter given code such as:
public string Name { get; private set; }
- Extension methods (adding methods to classes by including the
this
keyword in the first parameter of a method on anotherstatic
class):
public static class IntExtensions
{
public static void PrintPlusOne(this int x) { Console.WriteLine(x + 1); }
}
int foo = 0;
foo.PrintPlusOne();
- Partial methods: Allow codegenerators to generate method declarations as extension points that are only included in the source code compilation if someone actually implements it in another portion of a partial class.[6]
C# 3.0 was unveiled at the 2005 Professional Developers Conference. A preview with specifications is available from the Visual C# site at Microsoft. It is not currently standardized by any standards organisation, though it is expected that it will eventually become an ECMA and then ISO standard, as did its predecessors.
Microsoft has emphasized that the new language features of C# 3.0 will be available without any changes to the runtime. This means that C# 2.0 and 3.0 will be binary-compatible (CLI implementations compatible with 2.0 are able to run 3.0 applications directly).
Although the new features may only slightly change simple in-memory queries, such as List.FindAll
or List.RemoveAll
, the pattern used by LINQ allows for significant extension points to enable queries over different forms of data, both local and remote.
Preprocessor
C# features "preprocessor directives"[7] (though it does not have an actual preprocessor) based on the C preprocessor that allows programmers to define symbols but not macros. Conditionals such as #if
, #endif
, and #else
are also provided. Directives such as #region
give hints to editors for code folding.
XML documentation system
C#'s documentation system is similar to Java's Javadoc, but based on XML. Two methods of documentation are currently supported by the C# compiler.
Single-line comments, such as those commonly found in Visual Studio generated code, are indicated on a line beginning with ///
.
public class Foo
{
/// <summary>A summary of the method.</summary>
/// <param name="firstParam">A description of the parameter.</param>
/// <remarks>Remarks about the method.</remarks>
public static void Bar(int firstParam) {}
}
Multi-line comments, while defined in the version 1.0 language specification, were not supported until the .NET 1.1 release[8]. These comments are designated by /**.
public class Foo
{
/** <summary>A summary of the method.</summary>
* <param name="firstParam">A description of the parameter.</param>
* <remarks>Remarks about the method.</remarks> */
public static void Bar(int firstParam) {}
}
Syntax for documentation comments and their XML markup is defined in a non-normative annex of the ECMA C# standard. The same standard also defines rules for processing of such comments, and their transformation to a plain XML document with precise rules for mapping of CLI identifiers to their related documentation elements. This allows any C# IDE or other development tool to find documentation for any symbol in the code in a certain well-defined way.
Code libraries
The C# specification details a minimum set of types and class libraries that the compiler expects to have available and they define the basics required. In practice, C# is most often used with some implementation of the Common Language Infrastructure (CLI), which is standardized as ECMA-335 Common Language Infrastructure (CLI).
Hello world example
The following is a very simple C# program, a version of the classic "Hello world" example:
class ExampleClass
{
static void Main()
{
System.Console.WriteLine("Hello, world!");
}
}
The effect is to write the following text to the output console:
Hello, world!
Each line has a purpose:
class ExampleClass
Above is a class definition. Everything between the following pair of braces describes ExampleClass
.
static void Main()
This declares the class member method where the program begins execution. The .NET runtime calls the Main
method. (Note: Main
may also be called from elsewhere, e.g. from the code Main()
in another method of ExampleClass
.) The static
keyword makes the method accessible without an instance of ExampleClass
. Each console application's Main
entry point must be declared static
. Otherwise, the program would require an instance, but any instance would require a program. To avoid that irresolvable circular dependency, C# compilers run on console applications (like above) report an error if there is no static Main
method. The void
keyword declares that Main
has no return value. (See also, Side effect (computer science).)
System.Console.WriteLine("Hello, world!");
This line writes the output. Console
is a static class in the System
namespace. It provides an interface to the standard input, output, and error streams for console applications. The program calls the Console
method WriteLine
, which displays on the console a line with the argument, the string "Hello, world!"
.
Standardization
In August, 2000, Microsoft Corporation, Hewlett-Packard and Intel Corporation co-sponsored the submission of specifications for C# as well as the Common Language Infrastructure (CLI) to the international standardization organization ECMA.
In December 2001 , ECMA released ECMA-334 C# Language Specification. C# became an ISO standard in 2003 (ISO/IEC 23270). ECMA had previously adopted equivalent specifications as the 2nd edition of C#, in December, 2002.
In June 2005, ECMA approved edition 3 of the C# specification, and updated ECMA-334. Additions included partial classes, anonymous methods, nullable types, and generics (similar to C++ templates).
In July 2005, ECMA submitted the standards and related TRs to ISO/IEC JTC 1 via the latter's Fast-Track process. This process usually takes 6-9 months.
Criticism
- Performance
- As with other Virtual Machine-based languages, C# compilers typically use optimizations that are less developed than languages that compile directly into native code[9]. However, modern virtual machine technology often makes these penalties negligible for typical applications.[citation needed] Since C#’s Just in Time (JIT) Compiler, which does compile the program down to machine code, is expected to run fast it cannot use any optimizing techniques that are processor intensive[10] This sometimes makes a C# program slower than an equivalent C++ program. Also C# does a lot of parameter checking and Bounds Checking. Since most C/C++ libraries do not check parameters and expect valid inputs; the extra instructions that are run in C# for these checks cause performance degradation.[11] Generally C#’s programs are slow to startup because of the JIT compiling, but once the JIT Compiler has run C# programs generally run on par or a small percentage slower than any machine compiled program.[12]
- Language
- Although primitive types are treated as
Object
s at the source level, which allows programmers to use them easily in contexts where only Object
s are allowed (e.g., collections)[13], this is done under the hood in the language by boxing/unboxing them at the bytecode level. The creation of additional objects has a negative effect on performance. This also leads to a lot of pitfalls where the program's behavior is not the same as expected[14]. However, changes have been made since the introduction of generics in version 2.0[citation needed].
- Licensing
- Although the C# language definition is standardized under an ISO standard, only a part of the Base Class Library, which contains the fundamental functions that are used by all C# programs (IO, User Interface, Web services, ...) is also standardized. Furthermore, parts of the BCL has been patented by Microsoft[16][17], which may deter non-Microsoft implementations of the full framework.
Implementations
The de facto standard implementation of the C# language is Microsoft's C# compiler, included in every installation of .NET Framework, a version of which is used inside Microsoft's own Visual C# and Visual C# Express Edition IDEs, and directly invoked from Borland's C# Builder, and the open source SharpDevelop IDEs.
Alternative C# compilers are:
- Microsoft's Rotor project (currently called Shared Source Common Language Infrastructure) (licensed for educational and research use only) provides a shared source implementation of the CLR runtime and a C# compiler, and a subset of the required Common Language Infrastructure framework libraries in the ECMA specification.
- The Mono project provides an open source C# compiler (written in C# itself), a complete open source implementation of the Common Language Infrastructure including the required framework libraries as they appear in the ECMA specification, and a nearly complete[citation needed] implementation of the remaining Microsoft proprietary .NET class libraries (libraries not documented or included in the ECMA specification but are included in Microsoft's standard .NET Framework distribution).
- The DotGNU project also provides an open source C# compiler (written in C), a nearly complete implementation of the Common Language Infrastructure including the required framework libraries as they appear in the ECMA specification, and subset of some of the remaining Microsoft proprietary .NET class libraries (libraries not documented or included in the ECMA specification but are included in Microsoft's standard .NET Framework distribution).
Language name
According to the ECMA-334 C# Language Specification, section 6, Acronyms and abbreviations[18] 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".
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 ("#"). Microsoft's C# FAQ refers to the sharp symbol in the language name.[19] James Kovacs, a Microsoft MVP, explains some of the history of C#.[20]
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,[21] some (including from Microsoft's own material) incorrectly use the sharp symbol.[22]
The "sharp" suffix has been used by a number of other .NET languages that are variants of existing languages, including J# (Microsoft's implementation of Java), A# (from Ada), and F# (presumably from System F, the type system used by the ML family)[23]. The original implementation of Eiffel for .NET was called Eiffel#, a name since retired since the full Eiffel language is now supported. The suffix is also sometimes used for libraries, such as Gtk# (a .NET wrapper for GTK+ and other GNOME libraries), Cocoa# (a wrapper for Cocoa) and Qt# (a .NET language binding for the Qt toolkit).
See also
- C# and CLR
- C# Syntax
- Common Language Runtime
- Mono, an open source implementation of .NET
- Environments and tools
- Microsoft Visual Studio, IDE for C#
- SharpDevelop, an open-source C# IDE for Windows
- MonoDevelop, an open-source C# IDE for Linux
- Morfik C#, a C# to JavaScript compiler complete with IDE and framework for Web application development.
- Baltie, an educational IDE for students with little or no programming experience
- Related languages
- Comparison of programming languages
- Comparison of C# and Java
- Comparison of C# and Visual Basic .Net
- Cω programming language, extension to C#
- F#, Microsoft's version of OCaml
- Spec#
- Sing#
- Boo programming language, a cross between C# and Python
- IronPython, a Microsoft-supported, .NET-compliant version of Python
- Polyphonic C#
- C++/CLI
- Windows PowerShell, a .NET-based interactive command line shell/scripting environment
- Java
- C++
- Visual Basic
References
- ^ In Java 5.0, several features (foreach, autoboxing, varargs, attributes and enums) were introduced, after proving themselves useful in the C# language (with only minor differences in name and implementation). [1][2][3]
- ^ "Programming language history chart".
- ^ Tim Anderson (November 14 2006). "C# pulling ahead of Java - Lead architect paints rosy C# picture". Reg Developer. The Register. Retrieved 2007-01-20.
{{cite web}}
: Check date values in: |date=
(help)
- ^ "LINQ". Microsoft MSDN. 2007. Retrieved 2007-08-13.
- ^ [4]
- ^ "Partial Methods". Retrieved 2007-10-06.
- ^ [5]
- ^ Anson Horton (2007-09-11). "C# XML documentation comments FAQ". Retrieved 2007-12-11.
- ^ ."Why Java is Slow". 2007-12-06. Retrieved 2007-12-06.
{{cite web}}
: Check date values in: |date=
(help)
- ^ ."Harness the Features of C# to Power Your Scientific Computing Projects". 2007-10-29. Retrieved 2007-10-29.
{{cite web}}
: Check date values in: |date=
(help)
- ^ "Cost of array bounds checking -- one experiment". 2007-10-29. Retrieved 2007-10-29.
{{cite web}}
: Check date values in: |date=
(help)
- ^ "Microbenchmarking C++, C#, and Java". 2005-7-1. Retrieved 2007-10-29.
{{cite web}}
: Check date values in: |date=
(help)
- ^ See Unified type system
- ^ "C# FAQ - Boxing and unboxing". Retrieved 2007-09-08.
- ^ "Mono Project Roadmap: Mono_1.2". Novell. May 2007. Retrieved 2007-11-02.
Mono 2.0 will mark the time when the class libraries have complete support for the new features in the 2.0 edition of the framework.
{{cite web}}
: Check date values in: |date=
(help)
- ^ See .NET Framework
- ^ See Mono and Microsoft’s patents
- ^ Standard ECMA-334 C# Language Specification. 4 th edition (June 2006).
- ^ "Microsoft C# FAQ". Retrieved 2006-12-05.
- ^ "C#/.NET History Lesson". 2007-09-07.
- ^ Cover
- ^ Vsual C#.net 2003 boxshot
- ^ "Microsoft F# FAQ".
External links
Wikibooks has a book on the topic of: C Sharp Programming