使用现有代码(如使用其他语言、预先存在的 DLL 或 COM 项目编写的代码)时,这种方法非常有用。

double。

private fixed char name[30];

相反,该结构包含对元素的引用。

unsafe 代码块中时)中嵌入固定大小的数组的功能。

pathName 数组是对堆分配数组的引用:

public struct MyArray
    {
        public char[] pathName;
        private int reserved;
    }

fixedBuffer 数组有固定的大小。

fixedBuffer 实例固定到内存中的特定位置。

namespace FixedSizeBuffers
{
    internal unsafe struct MyBuffer
    {
        public fixed char fixedBuffer[128];
    }

    internal unsafe class MyClass
    {
        public MyBuffer myBuffer = default(MyBuffer);
    }

    internal class Program
    {
        static void Main()
        {
            MyClass myC = new MyClass();

            unsafe
            {
                // Pin the buffer to a fixed location in memory.
                fixed (char* charPtr = myC.myBuffer.fixedBuffer)
                {
                    *charPtr = 'A';
                }
            }
        }
    }
}

char 缓冲区中,每个字符始终占用两个字节,而与编码无关。

CharSet。

bool 数组不适合于创建位数组或缓冲区。

与所有不安全代码一样,请谨慎使用。

不安全缓冲区与常规数组在以下方面不同:

  • 不安全缓冲区只能用在不安全上下文中。

  • 不安全缓冲区始终是向量(或一维数组)。

  • char id[]。

  • 不安全缓冲区只能是不安全上下文中的结构的实例字段。

 

相关文章:

  • 2021-05-21
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2021-05-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2021-09-16
  • 2022-01-11
  • 2022-12-23
相关资源
相似解决方案