C# (see section on naming, pronunciation) is an object-oriented programming language developed by Microsoft as part of their .NET initiative, and later approved as a standard by ECMA and ISO. 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).
This article describes the language as defined in the ECMA and ISO standards, and avoids description of Microsoft's implementation. For a description of Microsoft's implementation, see Microsoft Visual C#.
Compared to C and C++, the language is restricted or enhanced in a number of ways, including but not limited to the following:
printf() function in C, all functions must be declared in classes. Classes are almost always organized into namespaces in order to prevent naming conflicts.
object class, and so all inherit the properties and methods of object. For example, every type has a ToString() method.
bool. Statements that take conditions, such as while and if, require an expression of a boolean type. While C and C++ do have a boolean type, it can be freely converted to and from integers, and expressions such as if(a) require only that a is convertable to bool, allowing a 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 exactly bool helps prevent certain types of programming mistakes.
IDisposable interface and the using statement, which together are an explicit form of "Resource Acquisition Is Initialization" (RAII).
file1:
public partial class MyClass1 { public MyClass1() { //implementation here } }
file2:
public partial class MyClass1 { //implementation here public Method1() { } }
yield keyword similar to yield in Python.
string status = ""; public string Status { get { return status; } internal set { status = value; } }
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).
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 ?? default(int); //can't assign null to int
from, where, select" keywords allowing to query from SQL, XML, collections, and more (Language Integrated Query LINQ)
Customer c = new Customer(); c.Name = "James"; can be written Customer c = new Customer { Name="James" };
listOfFoo.Where(delegate(Foo x) { return x.size > 10;}) can be written listOfFoo.Where(x => x.size > 10);
var x = "hello"; is interchangeable with string x = "hello";
var x = new { Name = "James" }
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).
Microsoft has emphasized that the new language features of C# 3.0 will be available without any changes to the .NET runtime. As a result, C# 2.0 and 3.0 will both be bytecode-compatible with the .NET framework 2.0. Although the new features may only slightly change simple existing queries, such as List.FindAll or List.RemoveAll, the Language Integrated Query system is adding significantly expanded semantics as well as a complex optimization and translation library. Indeed, a substantial portion of LINQ is DLINQ, the part of the framework that translates LINQ expressions into SQL database queries. Although it is asserted otherwise on some Internet forums, C# 3.0 has many significant and library improvements; it is not just syntax improvement. For example, the keyword var used for type inference is really necessary to enable any LINQ features; the types returned by LINQ queries are complex, and it would unwieldy to work with the LINQ system without var.
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, Web Services/Remoting (SOAP) 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).
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.
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.
ECMA specification 334 covers only the C# language. Programs written in C# commonly use the .NET framework, which is partly described by other specifications and is partly proprietary to Microsoft.
Microsoft released support of the 3rd edition of C# in the .NET SDK 2.0, and Visual Studio 2005, in November 2005.
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. The company takes an active role in marketing the language as part of its overall business strategies.
The de facto standard implementation of the C# language is Microsoft's Visual C#.
Microsoft's Rotor project provides a shared source implementation of the CLR runtime and a C# compiler.
The Mono project provides a CLR runtime, an implementation of the .NET libraries, and a C# compiler.
The Dot GNU project provides a CLR runtime, an implementation of the .NET libraries, and a C# compiler.
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 The "pound" symbol is known in every English speaking country (with the exception of North America) as the "hash" or Number_sign (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 http://www.microsoft.com/MSPress/books/imgt/5029.gif, some incorrectly uses the sharp symbol.
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). The suffix is also sometimes used for libraries, such as Gtk# (a .NET wrapper for GTK+) and Cocoa# (a wrapper for Cocoa).
One interpretation of the name C# is that it denotes an improved version of C, by analogy with the musical note, which is half a step above the C note. This is similar to the play on words used by the language name C++; "++" is a C operator that increases a variable by one.
.NET programming languages | C programming language family | Class-based programming languages | Statically-typed programming languages | Concurrent programming languages | Curly bracket programming languages | Programming languages
C šarp | C Sharp | C sostingut | Csharp | C Sharp | C-Sharp | C Sharp | C dieso (programlingvo) | C Sharp | سیشارپ | C sharp | C 샤프 | C sharp | C sharp | C sharp | C sharp | C Sharp programozási nyelv | C sharp | C Sharp | C Sharp | C Sharp | C sharp | C Sharp | C Sharp (programovací jazyk) | C Sharp | C sharp | C-sharp | ภาษาซีชาร์ป | C thăng | C Sharp programlama dili | C Sharp (мова програмування) | C#