views:

201

answers:

4

Possible Duplicate:
Differences between C++ and C#/.Net

I'm looking for things like:

  • C# can use strings for case statements whereas C++ can only use integers.
  • C# can only inherit from one class and multiple interfaces.

What are some other differences?

This seems like I should have found this question in SO but I couldn't. I apologize if it has been asked. Please provide link.

+4  A: 

Check out this link. It explains some key differences:

http://msdn.microsoft.com/en-us/library/yyaad03b(VS.71).aspx

AndHeCodedIt
C++ `long` is not 32bits.
wilhelmtell
Euh. Waay too superficial a comparison. You know what? I'd start the other way around. The one thing IN COMMON between C# and C++ is that both have the capital letter C in their name. Occasionally they both use braces to mean the same thing. That's it.
wilhelmtell
+14  A: 

The major difference is that they are completely different.

I think it's faster to list the similarities:

  • They both start with the letter C.
  • They both derive syntax from C.

The remaining similarities are mainly superficial.

  • They both have objects but in C# the objects are managed objects and are garbage collected, in C++ you have to manage memory yourself.
  • C++ supports multiple inheritance, C# does not (but it supports implementing multiple interfaces).
  • C++ has templates, C# has generics. The syntax looks similar, but under the covers they are very different.
  • C++ is generally compiled to native code, C# is generally compiled to intermediate language bytecode.
Mark Byers
As languages go, they are quite similar (not even talking about syntax here). Both are imperative languages with strong support for reuse through OOP and generic programming.
Ben Voigt
I would upvote you again for "They both start with the letter C"
klez
@Ben Voigt: I would say that C# is more of a multi-paradigm language than an imperative language. Although C# initially was mainly imperative, the addition of lambda functions, LINQ and the Task Parallel Library to C#/.NET shows that other paradigms are also very important. With C++0x, C++ is also moving forward in this area, but it is not released yet.
Mark Byers
Hm. I recently ported quite a lot of our code from C++ to C#, and this went very fast. It needed only some small corrections here and there. So stating they are "completely different" seems a little exaggerated.
Dimitri C.
@Dimitri C.: I tried porting some simple projects (about 1000 lines, no GUI) and it required changing almost every single line. Even after fixing the compile erorrs the resulting C# code initially didn't work because of some errors due to subtle differences between C# and C++ for the same syntax. I don't know what sort of project you were doing that was so easy to port, but my impression is that porting large projects is far from trivial - especially if you are converting idiomatic C++ to idiomatic C#, and not just writing both in some subset of C that almost works in both languages.
Mark Byers
@Mark Byers: I think it is odd that you had to make so much changes when porting. For clarity: the C++ code I ported did not rely on other libraries except the STL. Maybe it depends on the style you use when writing C++. Ours is almost identical to the C# style. Memory allocation is never done explicitly, instead we use scoped auto-deallocation and reference counted smart pointers. Of course, there are differences, but I still think a lot of C++ code is reasonably easy to port, provided the C++ code used a high level of abstraction, and that you port the libraries your app depends on with also
Dimitri C.
C# is still very oriented toward imperative programming. Currying, tail call optimizations, and other capabilities needed for programming in a functional style and very immature. Delegates and lambdas exist to make writing callbacks easier, and the fact that they're needed for FP really seems to be just an unintended bonus. In short: C# has features that are necessary for functional programming, but necessary != sufficient.
Ben Voigt
@Ben Voigt: Lambda syntax was not added to help make writing callbacks - it was added to allow LINQ syntax to be much cleaner. LINQ allows you to write code in a very declarative way. LINQ is powerful and versatile so lots of times before you would use a for loop and an imperative style, since C# 3.0 you can now use a LINQ query instead. You can also use the same syntax to query databases - again in a very declarative way, just as SQL is a declarative language. I don't think C++ has anything as good as LINQ yet but see http://stackoverflow.com/questions/232222/is-there-a-linq-library-for-c
Mark Byers
A: 

String in case statement is not that major... Garbage collection vs. direct memory management is.

alxx
+6  A: 
1c1
< C#
---
> C++
paxdiablo
Can someone please explain me the joke here? I don't get it :|
missingfaktor
@Missing Faktor, that's the output of the `diff` command from a file containing the line `C#` and another containing the line `C++`. Obscure, nerdy humour I know, but it's the best humour I've got :-)
paxdiablo