views:

373

answers:

8

I am a C++ programmer moving into C#. I worked with the language for a month now and understand many concepts.

What are some surprises i may get while moving from C++ to C#? i was warned about destructors not being executed as i intended. Recently i tried to do something with generics that would use T as the base class. That didnt work. I also had another problem but i'll chalk that up to inexperience in C#. I was also surprised that my app was eating ram, then i figure out i needed to use .dispose in one function. (i thought it would clean up like a smart pointer)

What else may surprise me?

Please no language bashing. I doubt anyone will but just in case...

+2  A: 

I think you've covered the main one. You should read up on garbage collection, understand why there are no destructors as such, figure out the IDisposable pattern (which kind of replaces destructors). I'd say that was the big one.

The only other thing I would say is to warn you the C# and the .Net Base Class Library are pretty big, to get the most out of it there is a lot to learn... Once you have covered the basics of garbage collection and the type system you'll want to look at LINQ, and you should take the time to explore the relevnt libraries / frameworks for your area (e.g. WPF, WCF, ASP.Net etc). But it's all good. I moved from C++ to C# and would never go back, I find it way more productive (I'm not bashing C++, I do still dable:-) )

Steve Haigh
A: 

Differences in the object model. For example value and reference types are separate by definition, not by how they are instantiated. This has some surprises, e.g.

myWinForm.Size.Width = 100;

will not change the width, you need to create a new Size instance and assign it.

Richard
Well, that won't compile either... the compiler spots this type of error. Besides, you'd just set myWinForm.Width ;-p
Marc Gravell
+2  A: 

Fortunately, Microsoft have some of that info here: C# for C++ Developers.

The struct vs class differences is another biggie for C++ origins.

Marc Gravell
A: 

C# code is usually much easier to read!

Adrian
Please generalise a little bit more !
bernhardrusch
LOL @ bernhardrusch
j_random_hacker
+1  A: 

I've made pretty much the same change some months ago (before that I've made a change to Java - but I didn't really spend much time programming Java).

Here are some of the biggest traps I've come across:

Attribute vs. Variable vs. Setter

One of the biggest traps I was stepping into was knowing if you have to change an attribute or set a variable or use a setter to set some aspect of a class.

IList vs. List vs. other collections

Know the difference between IList, List and all the other collections (IMO you can't really do much with an IList).

Generics do have their own pitfalls

And if you plan to use a lot of generics, maybe reading this helps you avoiding some of my errors: http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class/458753#458753

But in general I'd say that the change went pretty painlessly.

bernhardrusch
+1  A: 

Well, the languages are completely different as I'm sure you've realized if you've worked with C# for any time. You don't have a powerful macro or templating (I realize there are generics in C#) in C# as you do in C++. As far as memory, remember you aren't in a tightly controlled environment anymore. Expect to see a lot of memory usage in Task Manager and similar tools, this is normal. There are better, more fine-grained performance counters to see true memory usage. Also, you probably don't need to call dispose as much as you might think (by the way, check out "using" blocks if you haven't already).

Another clear one is the default constructor, in C# this does not create a new Foo object:

Foo myFoo;

You can't have anything like a "void pointer" unless you just think of that as being like having a reference of type object. As well, you need to think of Properties as syntactic sugar for methods and not public members as they look in C++ syntax.

Make sure you understand "out" and "ref" parameters.

Obviously this not a large list, just a few "pointers" (no pun intended).

BobbyShaftoe
+1  A: 

This is a rather big topic. A few thoughts:

C# is garbage collected. Doesn't mean you can stop paying attention about resource allocation, but in general you don't have to worry nearly as much about the most common resource: memory.

In C# Everything is an object. There are no "primitive" datatypes, even an int is an object.

C# has generics, not templates. Templates are far richer and more complex than C#'s similarly syntaxed generics, but generics still provide nearly all of the practical utility of templates, without many of the headaches.

C# has interfaces and single inheritance. Where you might look to multiple inheritance in C++, instead look to using interfaces or a different design pattern (e.g. strategy).

C# has delegates instead of function pointers. A delegate is basically just a typed function pointer. The use of delegates and delegate-relatives (lambda expressions, events, predicates, etc.) is very powerful and worth putting significant effort into studying.

C# supports yield return. This is very fundamental to the C# way of doing things. The most common form of iterating over some set is to use foreach. It's worth understanding how IEnumerable and iterators work.

Wedge
A: 

Some things that I have not seen mentioned that are not available in C++ and may be a bit surprising are attributes and reflection

Attributes as such do not give you full fledged AOP. However, they do allow you to solve a bunch of problems in a way that is very different to how you would solve them in C++.

Sam Saffron