| [Edit]
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#.
top
Language 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.
The language, and implementations thereof, should provide support for software engineering principles such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic garbage collection. Software robustness, durability, and programmer productivity are important.
The language is intended for use in developing software components suitable for deployment in distributed environments.
Source code portability is very important, as is programmer portability, 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.
top
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 CLR (Common Language Runtime) core. He can be cited in interviews and technical papers as stating flaws in most major programming languages, for example, C++, Java, Delphi, Smalltalk, were what drove the fundamentals of the CLR, which, in turn, drove the design of the C programming language itself. His expertise can be seen in C
top
Language features
C is, in some senses, the programming language which most directly reflects the underlying Common Language Infrastructure (CLI). It was designed specifically to take advantage of the features that the CLI provides. 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.
Compared to C and C++, the language is restricted or enhanced in a number of ways, including but not limited to the following:
There are no global variables. All methods and members must be declared as part of a class.
Local variables cannot shadow variables of the enclosing block, unlike C and C++. This is often treated as a potential cause of confusion and ambiguity in C++ texts, but C simply disallows this case.
Instead of functions being visible globally, such as the printf() function in C, all functions must be declared in classes.
All types, including primitives such as integers, are subclasses of the object class, and so all inherit the properties and methods of object. For example, every type has a ToString() method.
C supports a boolean type, 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 convertible 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.
Support for pointers with safety measures. Pointers can only be used within blocks specifically marked as unsafe, and programs with unsafe code in it need appropriate permissions to run. Most object access is done through safe references, which cannot be made invalid. An unsafe pointer can point to instance of a value-type, array, string, or a block of memory allocated on a stack. Code which 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 instead automatically garbage collected. Garbage collection addresses a common programming mistake of allocating memory and not releasing it, known as a memory leak. C also provides explicit control of unmanaged resources, such as database connections, through the IDisposable interface and the using statement, which together are an explicit form of "Resource Acquisition Is Initialization" (RAII).
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 (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. This is enforced at compile-time, during JIT, and, in some dynamic cases, at runtime. There are no implicit conversions between booleans and integers and between enumeration members and integers, 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).
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.
top
C# 2.0 new language features
New features in C for the .NET SDK 2.0 (corresponding to the 3rd edition of the ECMA 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
file2.cs:
public partial class MyClass
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, parameterized types are first-class objects in the CLI Virtual Machine, which allows for optimizations and preservation of the type information. Generics were initially designed and implemented by Microsoft Research, Cambridge.
Static classes that cannot be instantiated, and only allow static members. This is similar to the concept of module in many procedural languages.
The accessibility of property accessors can be set independently. Example:
string status = string.Empty;
public string Status
Nullable value types (denoted by a question mark, ie int? i = null;) which add null 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 SQL INTEGER NULL column type directly translates to the C
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 with property HasValue equal to false. When boxed, the Nullable instance itself was 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 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");
This was changed so that the value stored in Nullable is always boxed, so that null values always produce null references. 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).
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; // if i was null, initialize j with 0
top
C# 3.0 new language features
C 3.0 is the next version of the language as proposed by Microsoft. It includes new features driven largely by the introduction of the Language Integrated Query (LINQ) pattern:
Object initialization Customer c = new Customer(); c.Name = "James"; can be written Customer c = new Customer ;
Lambda expressions: listOfFoo.Where(delegate(Foo x) ) can be written listOfFoo.Where(x => x.size > 10);
Compiler-inferred translation of Lambda expressions to either strongly-typed function delegates or strongly-typed expression trees
Anonymous types: var x = new
Local variable type inference: var x = "hello"; is interchangeable with string x = "hello";. Aside from allowing this syntactic sugar -- which can be of great use when dealing with complex generic types -- it's required to allow the declaration of anonymously-typed variables (see above) because the true name of the type is known only to the compiler at compile time.
Extension methods (adding methods to classes by including the this keyword in the first parameter of a method on another static class):
public static class IntExtensions
int foo = 0;
foo.PrintPlusOne();
C 3.0 was unveiled at the PDC 2005. 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 bytecode-compatible.
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.
See also Language Integrated Query.
top
Preprocessor
C has a basic preprocessor based off of the C preprocessor that allows programmers to define symbols but not macros. Conditionals such as , , and are also provided. Directives such as give hints to editors for code folding.
top
XML Documentation System
C's documentation system is similar to Java's Javadoc, but based on XML. Multiline comments beginning with / and single line comments beginning with /// are treated as documentation.
public class DocumentedClass
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.
top
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. 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).
top
Hello world example
The following is a very simple C program, a version of the classic "Hello world" example:
public class ExampleClass
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.
top
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. C became an ISO standard in 2003 (ISO/IEC 23270). ECMA had previously adopted equivalent specifications as the 2nd edition of C
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.
top
Implementations
There are four known C
The de facto standard implementation of the C
The Mono project provides a C compiler, an implementation of the Common Language Infrastructure, and mostly compatible implementations of some of Microsoft proprietary .NET class libraries.
The Dot GNU project also provides a C compiler, an implementation of the Common Language Infrastructure, and mostly compatible implementations of some of Microsoft proprietary .NET class libraries.
Borland offers a professional C compiler, CBuilder, as well as a free version, Turbo C
top
Language name
According to the ECMA-334 C Language Specification, section 6, Acronyms and abbreviations 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 (""). 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 , 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 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) 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.
Another interpretation is that the sharp symbol represents 4 plus symbols together, C++ with another ++ on top.
top
See also
Mono, an open source implementation of .NET
|
|