C#: convert generic pointer to array

前端 未结 5 1651
长发绾君心
长发绾君心 2021-01-03 14:29

I want to convert a byte* to a byte[], but I also want to have a reusable function to do this:

public unsafe static T[] Create

        
5条回答
  •  悲哀的现实
    2021-01-03 14:53

    As of C# 7.3 this is possible as follows:

    public unsafe static T[] Create(T* ptr, int length) where T: unmanaged
    {
        T[] array = new T[length];
        for (int i = 0; i < length; i++)
            array[i] = ptr[i];
        return array;
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题