Wednesday, August 6, 2008

C# programming

Introduction

The C# 3.0 currently available in preliminary version is part of the LINQ project [1]. The aim of this project is to integrate better support for working data into mainstream general-purpose programming languages developed at Microsoft. It particularly targets the .NET platform, i.e. C# and VB.NET. Most of the ideas that are implemented in C# 3.0 are already available in mostly research and experimental languages that were previously developed at Microsoft Research. Among the most interesting and influential of these languages developed are Cω [2] and F# [3].

Contribution of F#

F# is a language based on ML, which is an impure functional language. In fact, it is based on a language called OCaml that adds several features to the standard ML. Functional languages are very different from imperative languages, with most of the widely used languages like C++, Java, and C# being imperative. The biggest contribution of F# is that it shows how functional languages can be compiled for the .NET Runtime (CLR) because the .NET CLR was initially designed for executing code written in imperative languages. Another aim of F# is the interoperability with other languages targeting the .NET platform. Part of the F# related research is also the ILX [6] project that shows how the .NET runtime could be extended to provide better support for functional languages, like first-class functions.

Functional programming, in general, was a big inspiration for some of the C# 3.0 features. The F# research language already showed how these features could be implemented for the .NET platform. C# 3.0 includes constructs that were inspired by:

  • Type inference: ability to deduce the type of expression
  • Tuples data types: types that represent a pair of values
  • First class functions: ability to take a function as a parameter and return it as a result
  • Lazy evaluation: ability to evaluate expressions only when it is needed later
  • Meta-programming: ability to manipulate with program source code

Most of these features that were added to C# 3.0 are very limited when compared with their implementation in F# and other functional languages. However, it is very interesting to see how functional concepts are becoming more and more important and can benefit the non-functional languages.

Contribution of Cω

Cω is a language based on C# and extends it in two important areas. The first area is better support for working with structured data (XML) and relational data (databases). The language extends the type system of C# to include support for several data types that are common in relational and structured data. It also provides querying capabilities for working with these data structures. The second area is support for concurrency constructs as a part of the language. In most of the widely used programming languages, support for concurrency is provided as a class library. By including these constructs in language, the program becomes more readable -- i.e. the intentions of the programmer can be expressed better -- and more work can be done automatically by the compiler.

C# 3.0 and LINQ project in general are inspired by the first area of extensions in Cω. The syntax extensions in C# 3.0 are very similar to the concepts developed in Cω. The biggest difference -- aside from the fact that Cω is an experimental language -- is that C# 3.0 provides better extensibility. This allows developers to provide a mechanism for working with different data sources. This extensibility is provided as a language feature and not as a compiler feature, as is the case with Cω. On the other hand, some extensions in Cω were simplified in C# 3.0. Anonymous types will be mentioned later, by way of example. So, where appropriate I will include examples where something possible in Cω can't be done in C# 3.0.

No comments: