C # In order to simplify development, the direct management of memory is hidden, but sometimes we also need direct memory operations, so unsafe CodeThe block came into being.
In general, we do not need to use the Unsafe code in the. NET Framework, but we need to use it in some application cases, as shown below:
Timely application Program: We may need pointers to provide program functions;
External Method: A pointer is required to be used as a parameter in a non-. Net DLL, for example, in a Windows API written in C language;
Debugging: Sometimes we need to check the memory content for debugging purposes, or you may need to write a program to analyze the status of a processing process or memory;
Next I will continue to introduce the conditions for using the Unsafe code. Due to the long digest, I will only give a brief description of the unsafe code. The following describes the most troublesome aspects in use, that is, the error encountered when using unsafe code. We may encounter this error:
Error cs0227: Unsafe code may only appear if compiling with/unsafe
In fact, it is not your code error. To use the Unsafe code in vs.net, we must set it in the properties of the project. The setting method is as follows:
Click project properties-> Configure properties-> Generate-> General: Insecure code is allowed (hooked up)
Now you can use the Unsafe code normally.