views:

1125

answers:

9

If asked, how would you describe the differences between C++ and C#/.Net

+11  A: 

C# is a managed language that runs in a virtual machine. It compiles down to a platform agnostic intermediate code.

C++ creates raw assemblies that run directly on the target platform

Because C# runs in a virtual machine, memory management is handled automatically. In C++, you must handle memory manually, or write (or acquire) a garbage collection library. However, great strides have gone into making C++ a little easier to use (Auto Pointers, and RAII).

C# does not support multiple inheritance, C++ does.

C# assemblies contain metadata about the code, allowing for runtime inspection. C++ does not contain this metadata, so the best you can do is fancy template programming.

The list could go on for hours :)

FlySwat
Although most of your remarks are usually true, they're not always. The C++ spec says little about compilation, and it can in fact be compiled for a virtual machine and have metadata, etc. (C++/CLI). The differences in the languages, as opposed to common implementations, are actually more striking
P Daddy
Type: C++ uses RAII (resource acquisition is initialization), not RIAA (Recording Industry Association of America).
Max Lybbert
Fixed the typo. Also, very true P Daddy.
FlySwat
+6  A: 

Totally different beasts with very similar syntax.

Big notes:

C++ is not, by default, managed. C# is.

C++ compiles down to machine code. C# 'compiles' down to CLR, which ASP.NET interprets (roughly) Edit - As pointed out in comments, it JIT compiles, not interprets.

C# has slightly more readable syntax, especially for newer developers.

C++ is faster.

Jeff
In what way does ASP.NET "interpret" CIL (not CLI, btw)? ASP.NET is a framework. You may mean the CLR, in which case that doesn't interpret either - it JIT compiles.
Jon Skeet
"The ASP.NET runtime interprets it" is what I'd meant. 'interprets' was a poor word choice, possibly. I know it's JIT compiled, but truthfully I'm still a bit fuzzy on the difference, and such a granular level of detail wasn't called for by the question.
Jeff
C++ being faster is also subjective...Sure in some cases, but not all.
FlySwat
The ASP.Net runtime is only involved when you write an ASP.Net webpage and use C# as the code behind.
Max Lybbert
C++ is no way faster except it is compiled and optimize for that platform. IL can JIT the code and optimize for that platform at any given time.
Jonathan Shepherd
+1  A: 

A big difference will be in the libraries. Microsoft has made a huge investment in the libraries for C#/.NET, making sure they're complete and robust. C++ is going to be a lot more haphazard.

Edit: OK, no love for this answer. Perhaps I just phrased it in a contentious manner. Realize that this is coming from a die-hard C++ lover, not from direct experience but from friends I have talked to who have made the switch. The thing that struck them the most was how much easier it was to do Windows programming with the C# libraries vs. MFC or the Windows API.

Yes, there may be more libraries available for C++, if only because it's been around so much longer. But they've been written with different philosphies in mind, and inconsistencies abound. Quick, which function do you use to do a case-insensitive comparison - is it stricmp, or strcasecmp, or CompareNoCase?

Mark Ransom
On the other hand, C++ probably leads by a wide margin in third-party libraries. They won't be as well integrated, but they'll probably do a lot more. Microsoft has only a small fraction of the smart developers in the world.
David Thornley
@David - and not only Microsoft makes C# libraries you know (but point still taken).
Mark Brackett
CLI C++ also can use all of the .NET framework libraries
FlySwat
+2  A: 

C++ is a language that runs on all sorts of platforms, being extremely popular on Unix and Unix-like systems. C#, while standardized, is rarely seen outside Windows.

This means that you will find a greater variety of support and additional libraries in C++, although they're not going to be nearly as well integrated as C# libraries. I'd strongly recommend becoming familiar with the Boost libraries for C++.

C++ also is something of an experimental language. Some of the things done nowadays were not foreseen by the standards committee, but developed out of more general things in the language (template metaprogramming comes to mind). This means that C++ is very powerful and extendable, but C# is almost certainly going to be easier to learn and use for most purposes.

I love C++, but it certainly isn't the best solution for every problem.

David Thornley
+9  A: 

C# uses a syntax somewhat based on C++, with presumably some borrowing from Java, which itself has roots in C++. It is an object oriented language, and takes part of its OO model from C++.

The similarities just about end here. Even a short list of differences would include (in no particular order and with no intended bias):

  • C# has a common type hierarchy, C++ does not. All types in C#, even primitives, descend from the class Object, or can act as if they do. (Value types, described below, conceptually derive from Object, but actually require some implicit conversion—known as boxing—to participate in the inheritance hierarchy.)
  • In C++ any type can be allocated on either the heap (via new) or on the stack. In C#, by contrast, there are reference types, which are always allocated on the heap (I'll ignore the stackalloc keyword for now), and value types which are always allocated on the stack. (Note that when I say "stack", this could also mean that the object's memory is directly stored within another type, in the case of members. What I really mean is that the object is directly accessible at its location, not indirectly accessible through a pointer.)
  • C++ has multiple inheritance (of implementation), C# does not (although it does have multiple inheritance of interface).
  • Speaking of which, C# has explicit keywords for interfaces and abstract base classes. C++ has those concepts, but they are not enforced by the language.
  • C++ includes a very powerful—and more complex and difficult to master—template meta language. C# originally had nothing like it, with many people believing that the common type hierarchy obviated the need for such techniques. Version 2 of the language (as well as of the framework) conceded this position somewhat with the introduction of generics which provide some of the same functionality of templates (and look very similar, too). C# generics, however, are a very different animal from C++ templates.
  • C# uses a garbage collector as part of the language spec. Many garbage collectors are available for C++, but this is not built into the language.
  • C# has delegates, which are a powerful alternative to C++'s function pointers and pointers-to-members.
  • C# has syntax for properties and events.
  • C# has syntax that enforces such things as non-overridable members, non-inheritable, and static classes. It also makes attempts to eliminate member hijacking with explicit modifiers such as override and new (as applied to members).

There are many more differences, some minor, some not. Microsoft has a comparison page on the two languages. Digital Mars used to have a good table detailing high-level differences, between their D language, C#, Java, and C++, but they seem to have pruned everything from the table except the D column, for some reason. A Google search, however, reveals many more detailed comparisons.

P Daddy
A: 

Come on... C++ does not necessarily compile to native (aka raw, machine code). C++ is a language spec.

What you'll expect from C++ is a highly flexible declarative language with features C# doesn't yet enjoy... such as class/function templates (similar, but different than generics), pointer manipulation, and more powerful operator overloading.

Many of C++'s features were said to be left out of the C# spec for fear they will be misunderstood and consequently misused.

C#, on the other hand, is a spec that focuses on higher level development and less on the nuts and bolts.

Though C# an only compile to portable .NET CLR assemblies, C++ is designed to be compiled to whatever target platform for which you can make a compiler. C++ is less portable than C# in the sense that what compiles great for native Win32 applications will not likely compile or run properly when recompiled to native Win64, or Linux elf, or CLR for that matter.

spoulson
C# can do native pointer manipulation, you just need to compile it with the /unsafe switch.
FlySwat
I loath how c# left stuff out for fear. For those of us who do understand. Means I have to write hybrid apps.I miss local statics most of all.
baash05
+2  A: 

Here is a list from Microsoft itself : http://msdn.microsoft.com/en-us/library/yyaad03b(VS.71).aspx

Ravi Vyas
+2  A: 

C# and C++ are completely different programming languages. The C++ programming language is defined by ISO/IEC 14882. You can read the latest draft of the ISO C++ standard at the link. C# is a proprietary language created by Microsoft. It is one of many ".NET" languages. C# works on Windows and only kind-of-sort-of on Mac and Linux using the Mono open source project. By contrast, C++ is an open standard, ISO standard programming language that is supported on pretty much every system. C++ code is generally translated directly to native code, whereas C# is generally translated to an intermediate bytecode format that is run on MSFT's "Common" Language Runtime virtual machine.

Michael Aaron Safyan
A: 

Both are general purpose object orientated languages but they differ in many ways. The biggest difference are:

  • C++ is compiled to machine code and C# compiles to an intermediate format called CLR that runs on a virtual machine
  • In C++ you have to do your own memory management while C# has a garbage collector
  • Many of the OO features differ between the languages, for example multiple inheritance.
leonm
C++ isn't just an OOP language.
GMan