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

后端 未结 11 2483
既然无缘
既然无缘 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:20

    Unsafe code is a fully supported function of the .NET CLR. The benefits are performance and compatibility with binary code. The runtime is a sandbox which prevents you from crashing and burning, but that comes with a cost. In situations where you are doing extremely intensive operations against large blobs in memory, for example image manipulation, it is faster to step outside the normal safety the runtime provides.

    That having been said, I think most people here would say "don't do it". The vast majority of .NET developers will not run into a case in their normal activities that can only be solved by using unsafe code.

提交回复
热议问题