I was fairly young when I first learned C++. It quickly became my favorite language by such a wide margin, that for many years, I shunned all other programming languages. For me, C++ was easy, high performance, portable, and powerful. Fast forward about 15 years, and here we are today. I was tasked with writing a fairly sophisticated web automation tool for a startup company. The tool needed to be ready as soon as possible, and that is how my journey to C#.

Why I chose to switch to C#

While I was very experienced with using C++ and MFC to create complicated GUI programs for Windows, I was not at all familiar with web automation. After searching endlessly on the internet for an easy solution that would suit my needs, it became overwhelmingly obvious that C# and .NET would be an easier, better supported solution for me because of the web browser control.

Why I fell in love with C#

After programming almost exclusively in C++ for so many years, I was nervous and skeptical at first. But C# won me over in a matter of only a few hours. Here are the biggest, coolest features that grabbed my attention.

Containers are built into C#

Sure, C++ has a wonderful standard library which includes things like the vector class, which are an invaluable tool. But in C#, everything is built in. List, Dictionary, Array, you name it. They also come complete with any sort of feature you can imagine. If you want to sort, reverse, or search, those functions are all available to you natively. Best of all, no more dealing with iterators, BOOST, and all the whacky syntax that comes with those features and tools. In C#, the functions work exactly like you would expect. And did I mention no more iterators? There’s also a great String class which is built in natively, and makes string manipulation very easy. Anything from searching for a substring, to using regular expressions, it’s all right there for you.

Foreach loops are oh so nice

I can’t tell you how many times I’ve written something like: for (int i=0; i < vMyList.size(); i++) { CMyClass *pCur = vMyList[i]; …}. What a handful to type every time you need to go through a list of objects stored in a vector. It’s no better with iterators. Bus in C#, you can type something like: foreach (CMyClass cur in vMyList) {…}. Wow! Not only is that so much shorter to type, it’s also easier to understand. Nothing in C++ even comes close to that convenience. The simple foreach keyword in C# is one of my favorite features that lets me develop applications faster, and with less error.

No more pointers…kind of

I’ve never had any problems with using pointers. They’ve always been a simple concept and easy to master, and my code is frequently littered with asterisks. In fact, it gets annoying typing asterisks all day. With C#, every object variable is basically an implicit pointer. That means that you know they’re pointers, the compiler knows they’re pointers, so the compiler doesn’t make you type asterisks at all. Everything is implicit and just works.

Garbage collection is awesome

In C++, whenever I declare a member variable that is a pointer, which is quite frequent, I also need to initialize it to NULL in the construction, then in the destructor function, I type something like “if (_pMyVar) delete _pMyVar;” What a hassle that is! One of the biggest features of C# is automatic garbage collection. It works well, and it’s incredibly convenient. You basically never need to worry about memory management, and you can focus on making your program do what you want it to do. No more chasing memory leaks, which can sometimes be a burden to track down and fix.

Summary

If I could some up C# in one word, it would be convenient. Everything is convenient. You don’t have to worry about pointers, garbage collection, iterators, or anything like that. Any sort of construct you can imagine is likely already a built-in type, with plain English member functions that do exactly what you think they’ll do. All of these features really help reduce the amount of typing you do, and can make your code more readable at the same time. This is an unusual combination, and I’ve got to say that Microsoft really did an outstanding job with C#. There are some other cools features of C# like properties, but coming from such a heavy C++ background, I didn’t find myself using those features. I’m sure in time, that’ll change. The only unfortunate thing with C# is that it isn’t a portable language. But if you’re just concerned with churning out a windows application quickly and with no hassle, C# should really be a consideration. As a side note, I’ve noticed that the auto complete feature in Visual Studio works much better for C# than it does in C++. I’m not sure if that was intentional or not on Microsoft’s part, but it often seems like auto complete knows what you are about to type, and that’s another huge productivity booster that I absolutely love.