Safe vs Unsafe code

2019-02-12 17:45发布

Read this question today about safe and unsafe code I then read about it in MSDN but I still don't understand it. Why would you want to use pointers in C#? Is this purely for speed?

标签: c# unsafe
4条回答
Summer. ? 凉城
2楼-- · 2019-02-12 18:30

I tend to avoid it, but there are some times when it is very helpful:

  • for performance working with raw buffers (graphics, etc)
  • needed for some unmanaged APIs (also pretty rare for me)
  • for cheating with data

For example of the last, I maintain some serialization code. Writing a float to a stream without having to use BitConverter.GetBytes (which creates an array each time) is painful - but I can cheat:

float f = ...;
int i = *(int*)&f;

Now I can use shift (>>) etc to write i much more easily than writing f would be (the bytes will be identical to if I had called BitConverter.GetBytes, plus I now control the endianness by how I choose to use shift).

查看更多
做自己的国王
3楼-- · 2019-02-12 18:32

There is at least one managed .Net API that often makes using pointers unavoidable. See SecureString and Marshal.SecureStringToGlobalAllocUnicode.

The only way to get the plain text value of a SecureString is to use one of the Marshal methods to copy it to unmanaged memory.

查看更多
叛逆
4楼-- · 2019-02-12 18:33

There are three reasons to use unsafe code:

  • APIs (as noted by John)
  • Getting actual memory address of data (e.g. access memory-mapped hardware)
  • Most efficient way to access and modify data (time-critical performance requirements)
查看更多
贪生不怕死
5楼-- · 2019-02-12 18:38

Sometimes you'll need pointers to interface your C# to the underlying operating system or other native code. You're strongly discouraged from doing so, as it is "unsafe" (natch).

There will be some very rare occasions where your performance is so CPU-bound that you need that minuscule extra bit of performance. My recommendation would be to write those CPU-intesive pieces in a separate module in assembler or C/C++, export an API, and have your .NET code call that API. An possible additional benefit is that you can put platform-specific code in the unmanaged module, and leave the .NET platform agnostic.

查看更多
登录 后发表回答