How to quickly zero out an array?

后端 未结 7 952
青春惊慌失措
青春惊慌失措 2020-12-08 18:30

I am currently doing it in a for loop, and I know in C there is the ZeroMemory API, however that doesn\'t seem to be available in C#. Nor does the somewhat equivalent Array.

7条回答
  •  温柔的废话
    2020-12-08 18:44

    Calling the method by using dll import.Its fast and easy to use :)

     [DllImport("msvcrt.dll", EntryPoint = "memset", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
     public static extern IntPtr MemSet(IntPtr dest, int c, int byteCount);
    

    c is the value you want to set in the memory

    OR

    [DllImport("kernel32.dll", EntryPoint="RtlZeroMemory")]
    public unsafe static extern bool ZeroMemory(byte* destination, int length);
    

    this only sets the given array to zero

提交回复
热议问题