Freeing developers from dealing with memory management has been touted as one of the principal advantages of managed code languages such as C#. Are there any studies/real-world examples of actual productivity gains of developing commercial applications in C# vs. C++?
I'm not sure if there are any studies, since the speed of development is largely influenced by many other factors like people's qualification, project managements, requirements change, internal politics etc. etc., but judging from my personal experience I can definitely say my productive rose tenfold.
It just gives you suddenly so much extra time when you don't have to worry about who's responsible for allocating and freeing memory withing a class, then between classes in a module, then between modules, then between applications, then how to implement this strategy in a consistent manner, then how to communicate it to all developers in a team or external contributors. You get lots of free time to implement useful functionality instead of still staying in the same place after many months trying to debug and clean up things.
Having said that I fell inclined to add that having a garbage collector does not excuse a developer from solid understanding of the low level operation of the CLR. An incompetent programmer without knowledge of value and reference types, of stack and heap management can still lay low an application in an innocent ignorance of the operational model.
I can definitely say my productivity has increased without having to worry about unsafe structures in C++, but that's just my experience. I feel that C# is much easier to work with than C++, but I've never seen an actual study, just my own experience.
I would say that the productivity gains by using C# as a language is the fact that you are working on top of the .NET Framework. This is where I find you get to be able to develop solutions faster.
I find that the main gain is the tooling, being able to set a conditional break-point that compares string values is something that VisualStudio still fails to do for C++ (even though dbx has supported it for ages). The reduced compile times help too.
I don't know about any studies, but from my experience, writing C# code is much faster. And memory management is not the only thing that speeds it up. In C# a lot of good things that make development faster:
- convenient iterating (foreach)
- standard string type
- standard string formatting
- built in conversions string<->numberic types
- reflection (no problem to create good object factory)
- enums that are easy to iterate, convert to string and vice versa
- built in analogs of boost::function and boost::signal that are easy to write, support and debug
- convenient lambda that helps to exploit algorithms
List is very long.
In my personal experience, automatic memory management has not really been a big factor. These days, there are a number of simple constructs in C++, like reference counted pointers, that can be used in the majority of cases to abstract memory management away. In fact, I would argue that it's more difficult in C# as it's sometimes very important to call Dispose on objects that implement IDisposable, but it's not always immediately obvious which objects are IDisposable.
On the other hand, I have benefited from C#'s other useful features. Things like events, the ability to alter my code in the debugger, using Designer (when it works) to set up a GUI, and "it just works" interoperation with native libraries (P/Invoke) or C++ code (C++/CLR) have made my life much easier.
I've been a C++ programmer for several years and now I'm a full-time C# programmer. To be totally honest, I don't think having automated memory management really makes me that much more productive because once you get use to it, it's really not such a big deal.
To me, the main productivity gain in C# is the .NET framework library and visual studio integration. The fact that intellisense actually works, able to using runtime reflection and having good date/time library really saves me a lot of time.
I don't know about studies. However, in my experience memory management in C++ is a negligible problem if you use common idioms such as RAII. In my opinion, the much more important productivity boost in C# comes from assisting tools. For example, there are no refactoring tools for C++ that really deal with all the constructs the language allows. In C#, Visual Studio already provides some nice tools, but Resharper is what I miss most in Visual C++. It's not that different in other C++ IDEs, since C++ is really hard to parse.
So if you compare productivity, also consider these effects. Adding the Boehm garbage collector to C++ might bring it to the same level as C#.
I would not trusat studies like that anyway. They all have som agenda they want to prove
and the parameters can always be modified to support any result.
The comparisons with memory management is most likely based C# vs C or C with classes.
Most current C++ project uses smart pointers and then memory is as easy as in
memory managed languages(altough a little more syntactiucally verbose)
The main productivity gain for C# is not in memory management but in its Visual
studio integration.
I have been in several big projects in both C++ and C# but contrary to most of
the other posters in this thread, my experience is that once the project has
matured, C++ has better productivity than C#. This is mainly due to its better
abstraction abilities, where you can hide more into libraries and remove more
boilerplate code.
From my point of view C# seems to be far more intuitive and contains a lot of functionality which C++ implements using 3rd party libraries such as Boost. Please note that not all C++ IDEs contain intellisense or something related to it while C#'s native IDE (MS VS) has it - and I must say it speeds up coding tremendously.
Also, C# 3.0 has got LINQ which seems to be an extremely convenient way of accessing data sources such as MS SQL Server, XML , CLR objects, regular arrays Lists Dictionaries etc.
From what I've been told C# 4.0 is going to implement additional data sources such as IBM's DB2, Oracle etc.
It's also worth saying that C# language itself is being developed more rapidly then C++ is, but the reason for that is pretty obvious.
Best regards
I'd be interested to hear from anyone with experience in both .NET and a good C++ library like Qt. I've not touched .NET for more than a few hours, but I'm loving Qt - the productivity gains over MFC, GTK, and wxWidgets is, IMO massive.
I've seen similar questions all over stackoverflow. Many people end up comparing the C++ language with C# language bundled with the .NET framework, which is clearly nonsensical. It's an easy mistake to make, since the two are often bundled together.
I'd also agree with Eric - automatic memory management isn't that big a deal for experienced programmers.
I too believe its the Visual Studio integration. It's one of the most powerful IDEs available in the market right now. The productivity can be increased further with use of tools like Resharper, CodeRush, AnkhSVN etc . It's awesome to see what these tools can do for u. Definitely got my productivity up.
I've been using C++ for several years before switching to C#. My C++ knowledge includes Boost, Loki, libsigc++ and of course the SC++L.
I don't think memory management was ever an issue in C++, productivity-wise. Any programmer worth his salt has a clear concept of ownership for any object and smart pointers made it effortless to work with shared objects.
If I had to choose between automatic garbage collection and deterministic finalization, it would be a hard choice. Both have their advantages. Garbage collection allows for lightning fast heap allocation and solves the problem of heap fragmentation. Deterministic finalization nearly eliminates the risk of unreleased resources and allows for ad-hoc RAII (see ScopeGuard).
What makes me more productive in C# is its reflection capabilities. This allows for agile development practices like unit testing and test coverage measurement, which are rather inconvenient to wield in C++. Using dependency injection in C++ is almost impossible, for example, and requires a lot of boilerplate code.
I also adopted a higher-level mindset in C#. I'm worrying more about object interactions than about how to best turn an algorithm into code. I can't say whether this is a natural progression or whether its due to .NET, but I feel the simpler programming model allows me to concentrate on algorithmic efficiency and less about implementation details.
Having worked a lot with both languages, I can confirm that GC is not a profuctivity booster. Any productivity gains I saw with C# were due to the rich .NET Framework support in the areas such as database access and XML processing.