Thinking Parallel

A Blog on Parallel Programming and Concurrency by Michael Suess

C++ vs. C# – a Checklist from a C++ Programmers Point of View

The NetI realize for most of you this is probably old news. I have been taking a first look at C# a little while ago, because I wanted to know if it is worth looking into, and because I am thinking about polishing up my class on Parallel Programming here at the university with a little language diversification. I have taught it using POSIX Threads before, but since everyone and their grandmother seems to use Java or C# these days, throwing in a couple of examples in these languages won’t hurt. And besides, I was curious ;). Without having written a single line of code and just by reading the various guides available, I have come up with a list of differences between C# and C++ and thought you might enjoy reading it here.

And so, without any further ado and very brief, here is my list of differences between the two, sorted into three categories: things I like better in C#, things I like better in C++ and things where I could not make up my mind. The last subheading lists the resources on the net used to compile that list:

Pro C#:

  • garbage collection
  • array bounds checking
  • huge .NET-Framework library
  • types have a defined size (e.g. a long is 64Bit)
  • strings are encoded in UTF/16
  • autoboxing – every type can be treated as if it inherits from object
  • supports constructor-chaining (one constructor can call another constructor from the same class)
  • when a virtual method is called in a constructor, the method in the most derived class is used
  • static constructors (run before the first instance of the class is created)
  • exceptions have access to a stack trace
  • advanced runtime type information and reflection
  • supports variadic functions nicely
  • built-in support for threads
  • no need for header files and #includes
  • no fall-through on switch-statements
  • arithmetic operations can be checked for overflow if required
  • objects must have a definite value before being used
  • attributes can be attached to classes and retrieved at runtime
  • no forward declarations required, classes can be arranged at will
  • access to class members / functions is done only by the dot (no more -> or ::)
  • conditional functions (e.g. for debugging)
  • structs and classes are actually different (structs are value types, have no default constructor in general cannot be derived from)
  • supports properties
  • readonly members are const, but can be changed in the constructor
  • finally block for exceptions
  • arrays are objects
  • support for anonymous functions
  • supports the base keyword for calling the overridden base class

Pro C++

  • better performance
  • portability
  • multiple inheritance
  • deterministic destruction (allows RAII)
  • any type can be thrown as exception (only classes derived from System.Exception in C#)
  • ability to enforce const-correctness
  • implicit interfaces on generics (in C#, generics must be constrained with an interface)
  • offers pointers (C# only offers pointers in unsafe mode)
  • support for macros
  • support for global variables, functions, constants
  • allows default arguments on function parameters
  • STL
  • supports bitfields

Where C# is just different from C++

  • value types and reference types exist (struct is value-type, class is reference-type)
  • value types live on the stack, reference types on the heap
  • references can point to null (must not be valid)
  • code is packaged in assemblies in C#
  • no automatic conversion from int to bool in C#
  • main-function is called Main in C#
  • no semicolon after a class declaration in C#
  • everything derives from object or can be treated as if

Resources

Disclaimer

As you can clearly see, this is not really comprehensive. The single bullet point huge .NET-Framework library would probably fill a page at least as big as this, if it was well presented. Some categorizations may also be controversial, e.g. I see portability as a huge plus for C++, while others might not care at all (or point me to Mono). Performance is also a point where I am not so sure. I expect all the nice features of C# to bring a performance-penalty into the equation, yet if you really see this in a real-world program is an entirely different matter. But since this is by no means a comprehensive treatment of the matter anyways, you are going to have to live with it – or if you can’t, you know how to use that comment button! 😛

Coming to the end of this article, I have a small request: I am looking for a good book on C# development to recommend to my students (and read myself, must not forget that :P), preferably one that is for people with a background in C/C++ and with content on threaded programming (because that’s what the class is all about). Browsing the reviews on the net, it appears that Programming C# is pretty good, judging from the table of contents it also covers threads, but of course I don’t know how well. Do you have any other recommendations? Or can you comment on whether the book would be suited for my purpose? Your comments are (as always) very appreciated!

149 Responses to C++ vs. C# – a Checklist from a C++ Programmers Point of View


Comments