C# 无法将带[]的索引应用于类型为';System.IntPtr';

C# 无法将带[]的索引应用于类型为';System.IntPtr';,c#,C#,我一直在读这是一个“不安全”代码,“IntPtr”通常不会以这种方式工作 有人能提出替代方案或解决方案吗 我的C#技能有限。谢谢你的帮助 for (num4 = 1; num4 < i; num4 += 2) { for (num = num4; num <= length; num += num5) { num2 = num + i; num11 = (num7 * numRef[(num2 - 1) * 8]) - (num8

我一直在读这是一个“不安全”代码,“IntPtr”通常不会以这种方式工作

有人能提出替代方案或解决方案吗

我的C#技能有限。谢谢你的帮助

for (num4 = 1; num4 < i; num4 += 2)
{  
    for (num = num4; num <= length; num += num5)
    {
        num2 = num + i;
        num11 = (num7 * numRef[(num2 - 1) * 8]) - (num8 * numRef[num2 * 8]);
        double num10 = (num7 * numRef[num2 * 8]) + (num8 * numRef[(num2 - 1) * 8]);
        numRef[(num2 - 1) * 8] = numRef[(num - 1) * 8] - num11;
        numRef[num2 * 8] = numRef[num * 8] - num10;
        IntPtr ptr1 = (IntPtr)(numRef + ((num - 1) * 8));
        //ptr1[0] += (IntPtr) num11;
        ptr1[0] += (IntPtr)num11;
        IntPtr ptr2 = (IntPtr)(numRef + (num * 8));
        //ptr2[0] += (IntPtr) num10;
        ptr2[0] += (IntPtr)num10;
    }

    num7 = (((num9 = num7) * num13) - (num8 * num14)) + num7;
    num8 = ((num8 * num13) + (num9 * num14)) + num8;
}
for(num4=1;num4对于(num=num4;num如果要在C#中使用指针算法,则需要使用不安全的和,如下所示:

public unsafe void Foo()
{
    int[] managedArray = new int[100000];

    // Pin the pointer on the managed heap 
    fixed (int * numRef = managedArray)
    {
        for (num4 = 1; num4 < i; num4 += 2)
        {  
            for (num = num4; num <= length; num += num5)
            {
                num2 = num + i;
                num11 = (num7 * numRef[(num2 - 1) * 8]) - (num8 * numRef[num2 * 8]);
                double num10 = (num7 * numRef[num2 * 8]) + (num8 * numRef[(num2 - 1) * 8]);

                // You can now index the pointer
                // and use pointer arithmetic 
                numRef[(num2 - 1) * 8] = numRef[(num - 1) * 8] - num11;
                numRef[num2 * 8] = numRef[num * 8] - num10;
                ... 
                int * offsetPtr = numRef + 100; 

                // Equivalent to setting numRef[105] = 5;
                offsetPtr[i+5] = 5;
            }

            num7 = (((num9 = num7) * num13) - (num8 * num14)) + num7;
            num8 = ((num8 * num13) + (num9 * num14)) + num8;
        }
    }
}
public不安全的void Foo()
{
int[]managedArray=新int[100000];
//将指针固定在托管堆上
已修复(int*numRef=managedArray)
{
对于(num4=1;num4对于(num=num4;num)你到底想用这段代码完成什么?这段代码是从哪里来的?如果你是C#新手,使用不安全的代码通常是不明智的,因为它……不安全。@BoltClock-我认为这段代码本身就是从地狱里冒出来的……这段代码看起来像反射器会吐出来的东西。。