tags:

views:

470

answers:

5
+2  Q: 

When to use C#/C++

Which areas of programming are each language best suited for?

I like both C++ and C# but i prefer to use C# because of .NET.

My question is when will you use C++ and when do you use C#?

So if you make a financial application for a company you will use C#? ( It's easy to design a form and connect to a database without downloading 3rd party libraries, and if you make advanced algorithms you would use C++ for it's speed?

This is my idea. I just watched video's @ http://www.academicearth.org, and it seems that universities prefer to use C++ for Machine Learning for example.

What do you guys think of it? and what is the industry view on this.

+2  A: 

Often we will use C# for front-end UI development, and C++ for server-side. One of the main reasons is portability/speed. People tell me mono for C# on linux/OSX is fast and stable, but from what I have seen that is not the case. YMMV.

daed
Mono works nice, if you use GTK# as gui, not windows forms. It's not that bad IMHO.
Tamás Szelei
I used Monodevelop and GTK# a long time ago, the dev environment was pretty nice but proved to be difficult when trying to port the GTK# code back over to windows. I should give it another shot.
daed
Gnome-Do is written in Mono. See yourself how performant and stable it is.
Ray
+11  A: 

Trying to compare the two isn't really fair -- C++ is usually used nowadays when you have the need for low-level, or portable, high performance code. You rarely find it used to implement business logic for enterprise systems, though about 10 years ago you did. You certainly wouldn't want to start out development with an enterprise system with C++ today unless you had a really, really good reason to.

But in academia, C and C++ have been used for many years, so it's probably not surprising that there is a large base of C++ code there for things like Machine Learning. C++ can often be faster than C# when it's optimized properly. It can run with less memory, on more platforms, and with fewer dependencies on large frameworks than C# can.

C#, however, is a lot more forgiving with its memory model, and typically has access to Microsoft or Mono's very large, comprehensive framework which allows developers to do a lot of stuff with minimal development effort, time, and cost. If you're working on the Microsoft platform, it is arguably the standard language nowadays.

Also see: http://stackoverflow.com/questions/316243/which-programming-language-to-learn-now/316256#316256

Dave Markle
Thank you for your answer.
ReDAeR
Agreed for the most part, though I'm not totally in line with grouping C and C++ into the same category. The *really* low-level stuff gets covered by pure C still and *not* C++ (e.g. kernels, drivers). Arguably, C++ covers a gap in power/easy-of-use that no longer exists with the advent of C# and other similar languages.
Noldorin
I should note that speed tests for the latest version of the C# compiler/CLR match or even beat C++ in various cases.
Noldorin
Do you have a reference for such a speed test? That would be useful for a number of people.
Eric J.
@Noldorin: Probably a fair statement. Now what in the world is up with Objective-C? Just when you think a language is dead and buried.... ;-)
Dave Markle
+6  A: 

Always use C# for business solutions. It's a more evolved language.

I worked for a company that does real-time authentication for PPV (like if you buy WWE or boxing on cable). They have huge volume right before the event. The team that worked on that part insisted that it had to be written in C++ for speed.

After a year of debating, we finally wrote a prototype replacement in C#. It turned out that the replacement was twice as fast.

There are VERY few problem domains where you need any of the optimizations you can do in C++ that are unavailable in C#, but C# code is much less error prone.

Eric J.
+1 to counteract the inevitable downvotes you'll get. But you're absolutely right.
Daniel Earwicker
Thank you for your answer aswell ( ofcourse all answers) It gives me more confidence to focus on C#
ReDAeR
The C++ 1 year old solution was outperformed 2 to 1 by a C# prototype?! I can't even imagine how weak the C++ programming must have been.
rpg
How can you say that without knowing any of the specifics of the problem domain? I might as well say "I can't believe your Ferrari was beat 2 to 1 by an H1" and not realizing that the course may have been offroad.
Eric J.
@Razvan - that is not actually an unusual result. Sadly your reaction is not unusual either. A C++ program, if written in the recommended safe style (using RAII, classes as value types, std containers and strings) typically performs worse than the equivalent C# program, mainly because the .NET GC performs so phenomenally well. To make the C++ program perform better requires a great deal of effort and expertise - far more than most projects can afford (or really need).
Daniel Earwicker
Eric- I can say that based on the C++ / C# characteristics: C# is a higher level more restrictive programming language compared to C++. If the C# code was faster, it means that the C++ code was bad, irrelevant of the problem domain.Earwicker: show me the benchmarks and the software. I'm tired of the same old GC is faster rah rah C# has JIT arguments.
rpg
@Razvan: The relevant point isn't how much performance the best C++ coder can get for a given problem vs. performance the best C# can get. The real (commercially meaningful) question is, how much performance can Joe programmer on my project get from C++ vs. C#? Writing optimal C++ code is far from trivial. E.g. not freeing memory at the right time can have a HUGE impact on performance. All of those C++ gains you get from not checking bounds (good thing?) are lost in one page fault. There's also the small problem of wiping memory through bad pointer use, but that's not a performance issue).
Eric J.
@Razvan: And here's an interesting debunking article regarding C++/C# performance: http://journal.stuffwithstuff.com/2009/01/03/debunking-c-vs-c-performance/
Eric J.
Eric, please visit the reddit discussion for your link. The debunking has been debunked... :)As for the cost/benefit of fine-tuning C++ code, it depends of course. As long as the C# works and is fast enough, use it, I don't have a problem with that.
rpg
@Razvan: Just because someone makes a post saying "that ain't so" doesn't mean the "debunking has been debunked". The fact is for most real-world, business applications C# performs nearly the same as C++. If you are writing the inner loop of a graphics rendering engine, you probably want to stick with a language that's closer to the underlying hardware such as C++/C/ASM.
Eric J.
+2  A: 

The one feature I like in C++ is the low-level access to memory and the control over memory-layout of structures. If you interface with even lower level APIs such as OpenGL, you usually interact with arrays of basic datatypes, such as floats. In C++, you can cast a void* texture to user defined types such as Image2D<PixelWithAlphaChannel<float, 4> > and use a fully object oriented interface for this otherwise unstructured dump of unspecified data.

If you cannot afford to keep multiple copies of low-level API data, C++ is the way to keep your code clean. However, you trade a lot of developer time for this. While in theory C++ allows for faster code (or at least the same speed, you could implement .NET in C++, but not vice versa), having tools such as Resharper and dotTrace can enable developers to improve the performance faster. So in practice, I'd opt for C# for both financial applications AND performance critical algorithms, IF they are not constrained by low-level APIs.

Malte Clasen
+1  A: 

From a commercial and support perspective, C# is still a one platform wonder. If you're not targetting the .NET runtime then it's not usually a realistic choice to use C#.

C++ is supported by a huge array of platforms, from embedded OS's to super-computers.

If you're targetting .NET, then C# is probably a more suitable language, there isn't really a good binding for C++, C++/CLI is more like a different or extended language in any case.

If there is a genuine choice, then the most overriding consideration should be the expertise available. Getting a bunch of C++ experts to develop in C# is a poor use of skills; writing a system in C++ with no C++ experts will probably result in a mess.

Charles Bailey