Surprises Moving from C++ to C#

半世苍凉 提交于 2019-12-05 02:48:33

问题


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


回答1:


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

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




回答2:


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




回答3:


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




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/780815/surprises-moving-from-c-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!