Should you use pointers (unsafe code) in C#?

后端 未结 11 2456
既然无缘
既然无缘 2020-12-05 02:15

Should you use pointers in your C# code? What are the benefits? Is it recommend by The Man (Microsoft)?

11条回答
  •  失恋的感觉
    2020-12-05 02:34

    You should use them if you need them; mostly this will be when dealing with some tricky interop scenarios (for example when I wrote a managed wrapper for DPAPI in .NET 1.0 they were needed) but very occasionally it might be to improve performance (after profiling!) by using stackalloc or similar.

    It is recommended by Microsoft in as much as they are the designers of C#, and they made the decision to add the capability to write unsafe code in it. You can see from the choice of keyword and the requirement to delineate the methods/classes in which you write it using the keyword, that it isn't designed to be the de-facto implementation choice.

提交回复
热议问题