views:

1154

answers:

13

Let assume one knows C++, what are the main concepts that can confuse them/that work differently when using C#?

I do not mean the syntax differences but for example how references works, what "replaces" pointers, etc. or how the idea of managed code affects developer experience?

EDIT: Thanks to Chris for pointing out this great blog posts:

http://blogs.msdn.com/texblog/archive/2005/06/02/424588.aspx
http://www.thinkingparallel.com/2007/03/06/c-vs-c-a-checklist-from-a-c-programmers-point-of-view/

EDIT2:

http://www.charlespetzold.com/dotnet/

+3  A: 

In C# every object is a reference. You never have to deal with pointers at all. Value types (int, float, bool...) are copied and objects are referenced. The only exception are structs which are value types in C#.

Value types can be referenced though using the "ref" keyword in the method declaration.

Also, .net has a garbage collector which automatically frees objects that are no longer used in your application. IMO in C# it is harder to keep track of where all of your memory goes. That is not to say that I don't like the language, however. .net also has a MASSIVE standard library which is well organized and can accomplish most of every task you can imagine.

C# is pure OO like Java - everything lives in a class unlike the psudo-OOness of C++.

nlaq
Picky detail: an object is _not_ a reference - a variable is.
xtofl
@xtofl: More picky details: a variable isn't a reference either. The *value* of a reference type variable is a reference.
Jon Skeet
I dont think C# is pure object oriented language. There are functional programming constructs(like lambda expressions etc) that are now part of the language and above all, there are primitives which mean that its not pure Object Oriented language.
Aamir
C# also has a MASSIVE standard library --> .NET, not C#...right?
moogs
Do you know any popular language that is "pure" object oriented?
Dave Van den Eynde
SmallTalk is "pure" OO, but not very popular any more. Of the popular ones, Ruby comes closest to that ideal. Of course, in practice it makes no difference - in fact it is better to have a "multiparadigm" language to work with.
Nemanja Trifunovic
+3  A: 

The meaning of "struct" and "class" is very different in C++ and C#. In C++, a struct is a class where the default access is public, and in C# a struct denotes a "value type" where a class is always a "reference type".

Dave Van den Eynde
Sounds very close to the colloquial definitions in C++.
David Thornley
+2  A: 

C++ templates and C# generics look very similar at first look, and actually serve mostly the same purposes, but the knowledge is less transferable than you'd imagine at first thought.

Robert Gould
Actually, Templates and Generics solve the same problem. Templates at compile time, whereas Generics at Run-time. It depends on where you want to take the hit.
Chris
The differences are not as simple as run-time vs. compile-time. Generics, while useful, are far more constrained than templates and certainly don't allow some of the more obscure and advanced things that templates do.
Joel
+11  A: 

c# apps run quicker by 6 months....

Tony Lambert
I think you mean C# applications are quicker to develop due to the simpler syntactical differences and a greater provision of libraries. You can't generalise a round figure such as "six months" either, it's entirely dependant on the application being developed.
Kezzer
@Kezzer: All true, but personally I think Anthony made his point more succinctly :)
Jon Skeet
and you ought to get a sense of humor in 2009.... ;-)
Tony Lambert
very well said :)
Aamir
:):):) I have to write that one down:)
Antonio Louro
LOL, I can't laugh in less than characters?
Chris
comment-golf: LOL in exactly 10 chars
annakata
Especially since C++ is by nature faster than similar C# code - if you get C++ to run significantly slower, you are doing it wrong.
coppro
+2  A: 

One of the main differences are the reference/value types in .NET. Classes are reference types, structs are value types. C++ programmers might think of references like strongly typed pointers, because the reference contains the address in the managed heap, like the pointer, except C++ heap is not managed of course. On the other side, value types are directly stored in the stack, no heap allocation there. And here comes another big concept in .NET - the heap is auto managed by the Garbage Collector (GC). The memory for all reference variables is allocated with the new operator like in C++, but freeing the memory is not responsibility of the programmer. When the memory is not referenced any more and the GC triggers, it's freed and the heap is defragmented. Again, this happens to reference types, value types are is the stack and after the method returns its stack is gone.
Added: For me the important thing is how this concept differences affect the development style generally. If we look at the usage of C++ and .NET, we see that C++ is used when performance is needed and .NET - when developer productivity is needed. Writing C# is easier, the large framework saves 90% (subjective of course) of the work, bugs are more rare. This makes it more suitable for so called enterprise solutions. But C++ is much better for creating drivers, embedded applications and 3D engines for example.

Rumen Georgiev
Clarification: value types are not always stored on the stack. If a value type is part of a reference type, it is stored on the heap along with the rest of the type.
Brian Rasmussen
+10  A: 

The intention of the languages are different.

C++ was designed as a systems programming language - close to the metal, where you don't pay for what you won't use. C# intends to offer a vast library to ease creation of applications.

This means you need a lot of code in C++ to obtain things you can do in one line in C# (or VB.Net or Boo or any emerging .NET language), but this also means you can optimize the code for a specific architecture.

C++ allows a mixture of OO, structured and generic (and meta-)programming. I'm not sure sure sure, but I believe C# generics are not fit for metaprogramming.

xtofl
I think that depends on your definition of "metaprogramming".
Dave Van den Eynde
Indeed I meant letting the compiler calculate the factorial of 20, explicit instantiation etc...
xtofl
+2  A: 

Constants

There is no concept of "const-correctness" in C#. Also, there are two versions of constants. Compile-time constants ("const") and runtime constants ("readonly")

Value Types and Reference Types

In C++ : All types are values, references are created to these values In C# : There are value types and reference types. This is a very important difference, as changing this will drastically affect the behavior of the code.

Dispose() pattern

For resource management (non-memory), instead of the RAII where we get resources in the constructor and release them in the destructor, one implements the Dispose pattern.

When using classes that implement the IDisposable interface, either:

a. call their Dispose() method or b. use the using keyword

using(ResourceHogger rh = new ResourceHogger())
{
   ... 
}
moogs
IDispose => IDisposable
rstevens
I don't necessarily agree with all of your answer, but your point about const correctness is a good one!
Dave Van den Eynde
@rstevens : thanks!@dave : thanks! i'd love to know which ones you don't agree with :)
moogs
+1  A: 

A somewhat surprising similarity to C++ is that in C# apps, despite being run in a garbage collected runtime, you can also leak memory, badly. ;)

macbirdie
leak? perhaps "not freed when you want it to be freed" is more precise?
moogs
Probably more like "not freed when you *assume* it to be freed", but it's still a memory leak for me. ;) After all memory usage grows uncontrollably until the application is terminated
macbirdie
+4  A: 

One of the main differences not mentioned yet is the single inheritence model (like in java), you can only inherit from one parent class.

Cohen
True, but anything in a C++ program that inherits from multiple parent classes gets my suspicious eye.
Dave Van den Eynde
In JAVA, an interface is used to simulate the multiple inheritance, without the bad side-effects. I'm pretty sure you have interfaces in C# as well.
Roalt
In C#, like in Java, you have single *implementation* inheritance.
Fabrizio C.
+5  A: 

Everything we can write for an answer has been written elsewhere, like here, or here

Chris
+2  A: 

If there's one thing that you need to be aware of when switching from C++ to C#:

Don't expect it to run on *nix (although you can give it a try)

On a more serious note, Microsoft may be changing their old ways, as indicated by the introduction Silverlight as cross-platform.

Andrew from NZSG
I think that with the current state of Mono that your comment is as invalid as can be.
Dave Van den Eynde
Neither completely true nor false - Mono is still not available for most high-end Unixes: Solaris, HP-UX, AIX...
Andrew from NZSG
+1  A: 

There is a resource out there that can be helpful: What the C or C++ Programmer Needs to Know About C# and the .NET Framework (aka .NET Book Zero) by Charles Petzold. ;-)

Fabrizio C.
+9  A: 

This is one of the questions where I'm really tempted just to say “wrong” – because basically, C# and C++ share (next to) no core concepts. Their names are intentionally similar, as is their syntax – but that's just a marketing trick.

C#'s core concepts say that it is an object-oriented, framework-supported language explicitly targeting Windows development. C# was developed to make RAD (rapid application development) in the tradition of VB6 possible, and offers facilities to work well with Smart Client (= classical windows) as well as web applications. Subsequent versions of C# have basically brought more of the same, albeit jumping on every marketing bandwagon along the way. Not that this is a bad thing, because it means that C# now has some really fancy features borrowed from functional programming. All in all, C# focuses on making programming even large systems relatively easy and safe (if something goes wrong, it does so on a high level; resources such as memory pointers are almost never corrupted).

C++ is fundamentally differently geared. C++ originally started out with the goal to be an object-oriented extension to C so there's at least some similarity to C# concepts: OOP. However, for at least 15 years, the development of C++ has parted from this path radically.

While OOP is still possible in C++, it is obviously no longer the main focus. C++ is called by its inventor (Stroustrup) “general purpose with a bias towards system programming” but that's rather unhelpful. C++ now really focuses on three main concepts: RAII (a very powerful mechanism to manage resources), template (meta-) programming and an unfortunate quasi-compatibility to C.

The latter is one of the reasons for C++' commercial success because it means that C++ can basically act as a drop-in replacement in large C systems that couldn't be rewritten from scratch. However, together with C++' meandering development pathway, it is also responsible for the convoluted syntax that is the target of a lot of (justified) criticism.

On the other hand, this also means that today C++ is in the rather unique position of offering a very high level of abstraction for the work with algorithms and data structures, at zero cost. Good C++ code can be just as efficient as good C or good FORTRAN code on the machine level (heck, even as efficient as good assembly code!).

Konrad Rudolph
Well said. I wish I could up-vote this answer more than once.
Milan Babuškov