Surprises Moving from C++ to C#

2019-03-30 07:46发布

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 didn't 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 figured 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...

7条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-30 08:01

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.

查看更多
Evening l夕情丶
3楼-- · 2019-03-30 08:02

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++.

查看更多
可以哭但决不认输i
4楼-- · 2019-03-30 08:04

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: Check if a class is derived from a generic class

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

查看更多
冷血范
5楼-- · 2019-03-30 08:07

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).

查看更多
ゆ 、 Hurt°
6楼-- · 2019-03-30 08:11

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

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

查看更多
Anthone
7楼-- · 2019-03-30 08:11

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.

查看更多
登录 后发表回答