C++ and C# Fundamentals

Working in .NET and C++

In my career, which kicked off in 1981, I’ve worked through four and a half seven-year changes. That’s my rule of thumb for measuring how long it takes for programming technologies to change and figure out how soon my current programming skills will become obsolete.

Back when I began in the early 1980s, Basic, COBOL, Fortran and Pascal were all jockeying for position, with Microsoft poised to make Basic very big in the burgeoning home computer business on Apple II, Tandy TRS-80 and Commodore PET.

In 1983 C++, the brainchild of Danish academic Bjarne Stroustrup, first appeared and rose rapidly in popularity thanks to introduction of fast, inexpensive compilers such as Turbo C++. It gained further traction after the adoption of the Standard Template Library (STL) in 1994, and has gained a new standard about every four years with the most recent being C++ 11

By the mid-90s, C++ ruled the roost, though Visual Basic and Turbo Pascal/Delphi knocked off some of its shine with their ease of development for Windows. C++ has no graphical interface and GUI developers on Windows had to use Microsoft Foundation Classes (MFC), which were slower.

C had been around much longer but only in the Unix world. Java appeared in the mid-90s and proceeded to rapidly conquer the business world and still dominates it. It was just easier to learn than C++ and left no worries about memory leaks. Then C# appeared at the turn of the century with the .NET framework, and has continued growing ever since. When it comes to jobs, you won’t go wrong if you learn C# or Java.

The .NET Framework

Sitting on top of Windows, .NET is a software framework with a very comprehensive library. It has a virtual machine called the Common Language Runtime (CLR) that’s very much like the Java Virtual Machine (jvm). Programs are compiled into Common Intermediate Language (CIL), which is portable and akin to Java’s bytecode.

When CIL code is loaded into memory, it is compiled into native machine code by a Just in Time Compiler and then executed.

Compared to Windows, the .NET framework provides an enormous amount of functionality that you don’t have to write yourself. So, for example, programs written in C# or VB.NET or any of the other 30 or so .NET CLI languages listed on Wikipedia don’t have to provide their own string handling. With .NET, this comes for free.

Memory management, database connectivity, Web application and network communications are also built into the .NET framework. This helps increase interoperability between different languages, as well as reduce the size of the executables.

.NET provides a managed environment, which means it’s almost impossible to write unsafe code that could overwrite the stack or cause programs to crash. The CLR ensures that only verified safe code can execute. That doesn’t mean you can’t write unsafe code, or make use of unmanaged code, but for that it requires you to mark your code as unsafe.

Included with .NET is an optimized Garbage Collector that reclaims memory once it’s no longer needed. Objects are tracked and once finished with, .NET tidies them neatly away. For those like me who spent much of their time developing for Windows, Linux or Mac before the managed code worlds of .NET and Java, the Garbage Collector means not having to manually free memory, and this is a big thing. No more hunting memory leaks.

C++ on .NET

It was natural to want C++ on .NET but it has been a failure despite two attempts. The first attempt was Managed C++, which was ditched and replaced with C++/CLI in 2004. The fundamental problem was trying to resolve the memory management model of C++ with the .NET managed environment. So the standard keyword “new” for allocating memory in C++ was replaced with “gcnew()”. This and other keywords made it a different language, and probably contributed to its failure.

Despite the advantages that C++/CLI brings – specifically better interoperability with unmanaged resources than most other .NET languages – it has not been successful. Nonetheless, it’s still included in Visual Studio 2012 and there’s a newer version C++/CX for Windows RT.

Programming Languages

C# is the clear winner for the most-used .NET programming language, with VB.NET in second place. Java was the obvious target for C# though its take-up in the Enterprise Application server world has made it a tough nut to crack. Java is still at least twice as popular as C# according to Tiobe, though its popularity has been in a long-term decline according to Google Trends.

The rise in popularity of Android phones will definitely help keep interest in Java going for a long time. Now if Windows Phone 8 could sell just as well, it would boost C#’s popularity.

The Web and Desktop, Too

Currently, approximately 112 million websites, about 1 in 6, are run by a version of Microsoft’s IIS web server, which can be programmed in C# or VB.NET using ASP.NET. This is a scripting technology driving WebForms which are quite similar to the desktop WinForms where each page is built with component controls and a page (code behind) for event handlers and other code.

C#/VB.NET Web developers also have a further technology with the ASP.NET MVC, which is short for Model View Controller. It’s a modern Object Oriented Design pattern that brings modern structure to websites.

The last five years has seen a new Microsoft technology intended to replace the gray screen boxes of WinForms with scalable, highly colored GUIs. These use the video card’s Graphical Processing Unit (GPU) hardware to render the screen when possible. This is Windows Presentation Foundation (WPF) and it harnesses a powerful version of XML called XAML to develop the desktop GUI in declarative form without a line of code.

Windows 8

C++ has gained a new lease of life in Windows 8, as it’s one of just four languages that can be used to create Windows Store Apps. The others are C#, Visual Basic and JavaScript. You get a choice of developing GUI apps in C++ with XAML or DirectX for games and high speed graphical applications.

C++ Elsewhere

In the Linux/Unix world, C++ is possibly the major compiled language, though C is still popular, and don’t rule out Java. Becoming good at C++ isn’t easy, though, and scripted languages like Python are certainly easier to learn.

The open source GCC (GNU Compiler Collection) has included an excellent C++ compiler for years though the Apple-funded Clang compiler front end and its associated LLVM compiler back end are starting to make inroads through faster compilation and technical superiority.

The games world is dominated by C++ for developing AAA games. No other programming language really comes close, and now it seems C++ can become a major language for developing browser games.

With a growing impetus to develop major game titles that run in the browser, the ability of Clang/LLVM to compile C/C++ and generate an optimized subset of JavaScript should not be overlooked. This was powerfully demonstrated in May 2013 with the compilation of a million lines of C++ to produce a very playable demo of the Unreal Engine that runs in  Firefox. It’s jaw-droppingly good.

So in the programming universe dominated largely by C# and Java, C++ still manages to stay not only relevant — but cool too.