Developer

Why you should move to C#

The migration to the .NET platform offers many development choices. This top 10 list explains why C# is an important offering and why you should consider moving to C#.


If you are moving to .NET and you already know C++ or Java, C# is a logical choice. However, if you are undecided about .NET, you might wonder why you’d want to move to C# rather than stick with your COM-based C++. Often the decision to move to .NET is based on factors other than learning C#, but C# does offer advantages over any COM-based language, thanks to the .NET Framework. This article examines 10 reasons to make the move to C#.

Modernized language
C# is a modernized version of C++. Originally you had the language C, which was widely used. C++ came about to add object-orientation to C, and C++ became the language of building “real” applications for Windows (according to the C++ developers.) C++ was used for writing the infrastructure and low-level applications, while Visual Basic developers wrote business applications.

C# brings the rapid development paradigm of VB to the world of C++ developers, with some obvious changes. C# takes advantage of the .NET Framework, which means you have access to a powerful forms engine, just like VB developers have had for years. New data types have been added, such as the decimal data type for performing financial calculations.

Type-safety
C# is type-safe, which means several things. For example, you cannot use uninitialized variables. In C++ it is easy to declare a variable and then check its value; whatever was in the memory address given to that variable would then be shown, and this could wreak havoc on an application. The C# complier will notify you if you try to use a variable before you have initialized it to some valid value.

With C#, you can no longer just walk past the end of an array, as you have been able to do in C and C++ for ages. In C++ you could declare an array of three elements and then happily examine the fourth element of that array and get the next chunk of memory.

Object-oriented
While many would argue that C++ is object-oriented, C# goes to another level. Even simple data types can be treated as objects, meaning that an int has methods associated with it. For example, you can use the ToString method to get a string value for an int, as shown below.
int Counter=14;
Console.Write(Counter.ToString());


In addition, literal strings can be treated as objects and support a variety of methods, such as Trim, ToUpper, ToLower, and many others, as shown here:
Console.Write("hello, world".ToUpper());

Simplified syntax
While C++ is an extremely powerful language, it has not typically been considered easy. C# attempts to simplify the syntax to be more consistent and more logical while also removing some of the more complex features of C++. For example, C# does away with pointers. As a type-safe language, C# doesn't allow direct memory manipulation, so pointers are no longer needed in C#.

Header files have also been removed from C#. The namespace and reference operators, :: and -> respectively, have been replaced with a single operator, the period (.).

Perhaps one of the biggest changes is that the int and bool data types are now completely different. This means that you will finally have an end to the assignment vs. comparison problem in if statements. In other words, the following code will not even compile under C#:
int Counter=14;
if (Counter=14) { //do something }


Attempting to compile this code will return an error stating:
Cannot implicitly convert type ‘int’ to ‘bool’

C# also removes memory management issues from the developer by using .NET’s garbage collection scheme. Items no longer referenced are marked for garbage collection, and the Framework can reclaim this memory as needed.

XML comments
C# supports the introduction of XML comments. Far from being just another way to add comments to code, XML comments can actually turn into your documentation. The comments are placed into XML format and can then be used as needed to document your code. This documentation can include example code, parameters, and references to other topics. It finally makes sense for a developer to document his or her code, because those comments can actually become documentation independent of the source code.

Not just Microsoft
C# is not just about Microsoft anymore. Microsoft released C# to ECMA (take that, Java programmers!) and it has been published as a standard. In addition, the Mono project is an effort to make an open source version of the .NET Framework (a subset of it called the CLI) and an open source version of C#, all for Linux. You can read more about Mono and download it here.

All about components
Creating components is straightforward, as is referencing those components in code. Namespaces in C# replace a lot of the headaches from the COM world in which you had to perform registry lookups and instantiate objects and worry about such things as IUnknown and IDispatch. Using C#, you simply import a namespace and then begin using the classes in that component—no registry lookups or COM plumbing required.

The power to be unsafe
Much is made about C#’s type-safety, but if you want to drive without a seatbelt, you are free to do so. If you really need to use pointers, for example, you can use the unsafe keyword to mark a block of code. This allows that code to bypass the Framework’s type-safety checking and means that you can directly manipulate memory. This makes C# incredibly powerful and is one of the advantages of C# over VB.NET.

Cross-language capabilities
C# has the ability to allow you to interoperate with any other language on the .NET platform. Much has been said about how you can create a component in one language and inherit and extend that component in another language, which is something that was difficult, if not impossible, with COM.

C# also supports the concept of error handling across different languages. Gone are the unfriendly HResults; instead, you have access to .NET exceptions, which are consistent across any .NET language.

It might be worth more
When it comes to creating applications for .NET, the choice between VB.NET and C# is typically not important. However, it is quite possible that companies are willing to pay more for C# developers; C++ developers have typically been harder to find than VB developers and have been better compensated. It's likely there will be more VB.NET developers than C# developers, so this trend may well continue into the future.

Editor's Picks