tags:

views:

154

answers:

4

any best practices?

+1  A: 

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

Wim Coenen
A better suggestion would be to point out where they might be useful rather than offering up such a blanket statement.
ChaosPandion
+1  A: 

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.

ho1
The wrapper class is a good idea. You really want to keep your unsafe code in one place.
ChaosPandion
Implement the Dispose **and** destructor pattern for when dispose is not called.
chibacity
+1  A: 

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.

KMan
A: 

In addition to the answer about implementing dispose for types that own pointers, implement the full Dispose\Destructor pattern as users of your API may not always call dispose:

http://www.bluebytesoftware.com/blog/CategoryView,category,DesignGuideline.aspx

chibacity