开发者

Working with pointers in C#

How can I work with pointer开发者_开发知识库s?


Enough has been said about using the pointers in C#; nevertheless must if you use, here is an example of how you can do that.


In addition to the other answer that already points out that pointers and unsafe code should be avoided if possible.

You want to avoid having unsafe code spread out all over your code base so I'd suggest writing a .Net wrapper on top of all your unsafe calls and that way you only need to worry about it in one place. Possibly even create a class library for it, but that depends on what exactly you're doing.

It will obviously be very important that whoever uses the wrapper remembers to call the wrapper's Dispose methods and similar to make sure that any pointers or other unmanaged resources are disposed of properly, but that's not different from the rest of your code.


The best practice is to avoid unsafe code. So don't use pointers in C#.


I know that this topic is old but i found myself after swiching from C++ to C# in simulate behawior of pointer in some case just by using set/get properties. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜