English is not my mother tongue, this page may contain incorrect words or sentences.

urriellu.net => Articles => Software => Advantages of C#

Why C#?

Talking to people about C# what I see is that they don't like changes. That's common and that happens to all of us, but spending some time playing around with C# and if you know Java or C++ (for example) learning C# is very easy, but... why should you learn it?.

Note: I support C# when developing desktop applications and maybe games, but not for embedded systems (such as microcontrollers), real time apps or kernels, so the following advantages are only applicable to desktop apps.

Update: I'm working on Pigmeo Project, which allows to to easily develop applications (real time and efficiently) written in any .NET language (including C#) for microcontrollers.

Advantages over C and C++

  • It is compiled to an intermediate language (CIL) indepently of the language it was developed or the target architecture and operating system
  • Automatic garbage collection
  • Pointers no longer needed (but optional)
  • Reflection capabilities
  • Don't need to worry about header files ".h"
  • Definition of classes and functions can be done in any order
  • Declaration of functions and classes not needed
  • Unexisting circular dependencies
  • Classes can be defined within classes
  • There are no global functions or variables, everything belongs to a class
  • All the variables are initialized to their default values before being used (this is automatic by default but can be done manually using static constructors)
  • You can't use non-boolean variables (integers, floats...) as conditions. This is much more clean and less error prone
  • Apps can be executed within a restricted sandbox

Advantages over C++ and java

  • Formalized concept of get-set methods, so the code becomes more legible
  • More clean events management (using delegates)

Advantages over java

  • Usually it is much more efficient than java and runs faster
  • CIL (Common (.NET) Intermediate Language) is a standard language, while java bytecodes aren't
  • It has more primitive types (value types), including unsigned numeric types
  • Indexers let you access objects as if they were arrays
  • Conditional compilation
  • Simplified multithreading
  • Operator overloading. It can make development a bit trickier but they are optional and sometimes very useful
  • (limited) use of pointers if you really need them, as when calling unmanaged (native) libraries which doesn't run on top of the virtual machine (CLR)

Subjectively

If you write code in your free time you want to feel good doing it. If you enjoy programming, when you gain a bit of experience using C# you'll like it. After all technical advantages you can see what is most important is that you enjoy writing C# code, and thanks to the great amount of tiny details that helps you when using IDEs like Visual Studio, SharpDevelop, Monodevelop... you'll write code much more robust, clean, faster and comfortably than when doing it in other languages.

Myths and truths

  • "It is a Microsoft's property" - No! Microsoft developed it, but C# and .NET Framework are ECMA (2) and ISO (2) standards. In the other hand are ADO.NET, ASP.NET and Windows.Forms that they are not part of the standard (but supported by free implementations), and Microsoft has some patents in USA about C# and the CLI but other implementations of the virtual machine and compilers can be developed freely thanks to these therms.
  • "It is intended to be used only in Windows". That's true for Visual Studio, but that's just an IDE, and for Windows.Forms which is not part of any standard. You can write console-based multiplatform applications or even apps with GUI using GTK+ (see GTK#), QT/KDE (see Qyoto/Kimono) or other ghrapic libraries, which don't require any Windows-related library (most of the .NET Framework doesn't need anything related to Windows, and you can use it on any operating system). For example, new applications are being developed every day for GNOME running on Mono.
  • "It just runs on Windows". "On linux runs so bad". Mono has a .NET virtual machine implementation of versions 1.1, 2.0 (generics, partial classes, iterators, anonymous delegates...) and 3.5 (LINQ, anonymous types, lambda expressions, extension methods...) and includes a C# compiler. It is free software and runs on a bunch of operating systems and architectures. It has bindings for GTK+, OpenGL (TAO), LDAP, Cairo, CORBA... and supports the entire WinForms 1.1 and 2.0. On Linux (using Mono) you can also use some other languages such as Boo, Java, Nemerle, Visual Basic .NET...