Coding the .NET Framework
By Tim Anderson
Tim Anderson looks at C#, Visual Basic and the other languages that target Microsoft’s .NET Framework.
HardCopy Issue: 39 | Found In: Development | Published: 01/02/2008 | Last Revision: 06/04/2010
The .NET Framework is designed to support multiple languages which is why its core component is called the Common Language Runtime (CLR). Under the .NET Framework, your source code is initially compiled to an intermediate format called MSIL (Microsoft Intermediate Language). The first time your application runs the CLR further compiles the MSIL into native code which is used for execution from then on. Microsoft’s idea is that developers should use whatever language they prefer, although there are some technical limitations.
The C# language
Microsoft invented a new language solely for .NET and used it to write most of the Framework libraries. This is C# which is arguably the native language of .NET. C# is designed to look familiar to C programmers, although it borrows ideas from at least two other languages. One is Java which is also based loosely on C. Like Java, C# has automatic memory management, inheritance and interfaces (but not multiple inheritance), namespaces to resolve naming conflicts, strong typing and a huge class library.
What is the .NET Framework?
Microsoft has two main programming platforms today. The first is C++ which remains the language of choice for most of the Windows operating system itself, and for desktop applications when performance and broad compatibility matters more than productivity. The second is the .NET Framework which is for applications that are managed by the .NET runtime engine, either on the desktop or Web servers.
Announced in July 2000, the .NET Framework was in part an answer to Sun’s Java language and platform, after Microsoft’s efforts to use Java itself ended in a predictable legal tangle. The .NET Framework also enabled Microsoft to move on from Visual Basic 6, a popular tool but too closely tied to COM and ActiveX and in dire need of updating to support important language features such as inheritance and Try..Catch exception handling.
Other aspects of .NET include device programming through the Compact Framework and later the .NET Micro Framework for embedded work, Web development in ASP.NET, and integrated support for XML Web services. Today, with .NET 3.5, the scope has broadened further. Silverlight is a cross-platform .NET runtime that runs as a browser plug-in, while Windows Presentation Foundation (WPF) is an entirely new graphical user interface framework for the Windows desktop.
Another significant .NET platform is SharePoint, a collaboration server which supports document sharing workflow, and Web 2.0 features like wikis and blogs. In a nutshell, if you are building applications for Windows, it is likely that you will be using .NET.
Another influence on C# was Delphi, Borland’s object-oriented version of Pascal. Anders Hejlsberg, C# architect at Microsoft, had previously been Delphi’s architect at Borland. Delphi may have inspired C#’s support for component-oriented programming, including first-class support for properties and the ability to play nicely with visual designers.
C# is in some ways less strict than Java. Platform Invoke makes it easy to call external functions such as the native Windows API. Unlike Java, C# supports pointers provided they are used in a block of code marked as unsafe.
Released in 2005, C# 2.0 added generics (also known as parameterised types). This enabled type-safety and improved performance in general-purpose data structures such as arrays and collections. C# 2.0 also adds anonymous methods, partial classes and nullable types.
The full value of these extensions did not emerge until C# 3.0 in 2007. The big new feature in C# 3.0 is LINQ (Language Integrated Query) which adds query operators to the language. You can now query databases and process the results without needing to embed SQL strings in your code. LINQ has huge potential for simplifying database and XML applications. Other new features in C# 3.0 include implicit typing, where local variables can infer their type from how they are initialised, and extension methods, a convenient new form of static methods.
C# offers an excellent balance of power, flexibility, safety and productivity, and is a relatively easy transition from Java. It is the closest thing to a native .NET language and has great official and community support. XNA, the managed framework for games programming, only supports C#. In the jobs market, demand for C# is higher than for other .NET languages. That said, C# is case-sensitive and, with its braces and semi-colons, C is a little more intimidating than Visual Basic for beginners and non-specialists.
Visual Basic
Microsoft and BASIC go back a long way. A variant of the language was one of the company’s first products and it was bundled with MS-DOS, the foundation of Microsoft’s success. Visual Basic was a ground-breaking product which greatly boosted Windows as a platform for custom business applications.
Visual Studio 2008
Microsoft has worked hard over the years to unite its development tools into one integrated environment, and now it has just about succeeded. Aside from a few stalwarts working in Visual FoxPro, Visual Studio is now where it all happens, whether targeting device, desktop or Web. The range of project types in a full installation is remarkable and can include standard Windows or Web applications in a choice of languages, Workflow applications based on SharePoint, add-in projects for Microsoft Office, Silverlight applications, setup projects, reporting projects, and a range of native code projects for Visual C++.
Since Visual Studio 2005, Visual Studio has also become a collaboration platform. The various components of Team Suite include source control (much improved over the old SourceSafe), guidance for various development methodologies, bug and work item tracking, unit testing, load testing, build management, and a SharePoint project portal which tracks progress and supports discussions and shared documents.
Visual Studio 2008 is a strong recent release and adds designers for Windows Presentation Foundation (WPF) and Windows Communication Foundation (WCF), full support for LINQ including an object relational designer, ASP.NET AJAX projects and the ability to target multiple versions of the .NET Framework, including the latest version 3.5. It is worth noting that Visual Studio now has a complementary design tool, Expression Blend which lets designers work with the same WPF solution but in a separate tool tailored to their needs.
Finally, Visual Studio is now easier for third-parties to extend. The Visual Studio SDK is now free and licensing restrictions have been lifted, which means we will see more independent projects such as Ruby in Steel which extends Visual Studio to support Ruby on Rails development.
Visual Studio 2008, showing the designer for Windows Presentation Foundation.
When the .NET Framework was launched, Microsoft made Visual Basic .NET a near-equal partner with C# as a language that can target the platform. It retains the English-like syntax and case-insensitivity that makes the language approachable. Unfortunately the differences between COM and .NET made full backwards compatibility impossible, and the difficulty in migrating old-style Visual Basic applications to .NET has been painful for developers.
Seven years on from the launch of .NET, this is now less of an issue, and Visual Basic remains popular. Microsoft reports that Visual Basic Express is the leader among Microsoft’s free programming tools, with the number of downloads far exceeding those for C# Express.
Like C#, Visual Basic has evolved in step with the underlying .NET Framework. Visual Basic 2005 introduced generics, nullable types and a C#-like Using statement for guaranteed disposal of system resources.
Visual Basic 2008 matches C# 3.0 with integrated query operations, type inference, extension methods and Lambda Expressions. There are also XML literals, a powerful extension to the language that simplifies parsing and generating XML in your code. Visual Basic is quick to learn and easy to use, making it ideal for non-specialists. In most scenarios it is equally as capable as C#, so it is also suitable for professional developers who happen to prefer its syntax or who have existing Visual Basic skills. The trend towards dynamic languages has given Visual Basic a boost, as it has always supported dynamic typing. However Visual Basic does have one or two annoyances, such as its awkward line continuation symbol, and not everyone likes case-insensitivity. C# has the edge in a few areas, such as the ability to use pointers. There are plenty of VB jobs available, but not quite as many as for C#.
Other .NET languages
Although the majority of .NET development is done with C# or Visual Basic, there is growing interest in alternatives. In part this is driven by the emergence of dynamic languages, such as Python and Ruby, which arguably offer greater productivity and flexibility than traditional languages with static typing.
IronPython is a well-advanced implementation of Python for .NET, while IronRuby is a version of Ruby which is in its early Alpha stage. In future both will use the DLR (Dynamic Language Runtime), a new set of services for the CLR which support dynamic languages. Visual Basic is also expected to use the DLR in future versions. Microsoft has also announced Managed JScript, a new implementation of JavaScript 3.0 which will also use the DLR. One of the main uses for the DLR is likely to be within Silverlight 2.0, the forthcoming version of Silverlight that includes a cross-platform CLR.
Along with these more experimental languages, Microsoft has always supported C++ as a .NET language. Originally this was through a set of extensions called Managed C++ but in Visual Studio 2005 and later this has been replaced by C++/CLI, which is both simplified and more powerful. C++/CLI has an obvious appeal for those with existing C++ skills, and also offers the deepest available interoperability between managed and unmanaged code. J# is Microsoft’s implementation of the Java language for .NET, but this only appears in versions of Visual Studio up to and including 2005 and will not be developed further.
How to shoot yourself in the foot in Visual Basic:
'You only appear to have shot yourself in the foot but you had so much fun doing it that you don’t care.