开发者

Upgrading from C++ to C#

开发者 https://www.devze.com 2023-01-20 19:04 出处:网络
I have a large application written in C++, For various reasons I am going to rewrite it in C#, I have done plenty of Delphi to C#, VB to C# but C++ to C# I have never done, although I am competent in

I have a large application written in C++, For various reasons I am going to rewrite it in C#, I have done plenty of Delphi to C#, VB to C# but C++ to C# I have never done, although I am competent in C++ I want this to be as smooth a conversion as possible.

Mainly what I am asking is what pitfalls await me in this conversion is there any key areas I should be aware of or any advice you can provide me.

This article is quite good, but is there anything else I should be weary of?

http://msdn.microsoft.com/en-us/mag开发者_运维知识库azine/cc301520.aspx


Main pitfall - do not think it's an upgrade. These are DIFFERENT languages, and in many places you will need complete different approach to the problems. So you should think reimplementation with minimal code reuse.

This article is decent.


I'd advice you to pay attention to the objects lifecycle.

In C++ you destroy objects explicitly when you've done with them. In C# (.NET) you don't. It can happen that an object holds on to some important resource (file handle, database connection etc.). If it is an issue, make use of the using directive.


You need to translate the spirit of the code, but not the code itself. You need to leave behind all the things you had to do in C++ because that was how it was done there. Good translation is highly creative process, so be creative.


The handling of strings was a pitfall for me at the beginning. While I Visual C++ you use pointers the methods in C# have indeed return values.

dummy = dummy.Replace("a", "b");


If you have C++ dll and you want to use them in your C# project you can used them by pinvoke and DllImport

There will be lot of differences while you will try to convert or rewrite unmanaged code to managed one. Here is a C++ to C# converter which is quite good for converting your c++ code to C# , though you can not expect to convert the whole project using it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号