Partager via


Fixed Size Buffers (C# Programming Guide) 

The ability to create a data structure with a fixed size is useful when working with existing code, such as code written in other languages, pre-existing DLLs or COM projects.

[attributes][modifiers]fixed type identifier [expression];

Parameters

Parameter Description

attributes (optional)

Optional declarative information. For more information on attributes and attribute classes, see Attributes.

modifiers (optional)

Optional modifiers that include the new modifier and one of the four access modifiers.

type

One of the types: bool, byte, char, short, int, long, sbyte, ushort, uint, ulong, float, or double.

identifier

The name of the array.

expression

The expression that evaluates to the size of the array.

For example, the following statement declares a fixed size buffer (name) of length 30: fixed char name[30];

Remarks

In previous versions of C#, declaring a C++ style fixed-size structure was difficult because a C# struct containing an array does not contain the array elements, but instead contains a reference to the elements.

C# 2.0 adds the ability to embed an array of fixed size in a struct when used in an unsafe code block.

For example, prior to C# 2.0, the following struct would be 8 bytes in size where the pathName array is a reference to the heap-allocated array:

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

In C# 2.0, a struct can be declared with an embedded array:

public struct MyArray // This code must appear in an unsafe block
{
    public fixed char pathName[128];
}

In this structure, the pathName array is of fixed size and location, and can therefore be used with other unsafe code.

The size of the 128 element char array is 256 bytes. Fixed size char buffers always take two bytes per character, irrespective of the encoding. This is true even when char buffers are marshaled to API methods or structs with CharSet = CharSet.Auto or CharSet = CharSet.Ansi. For more information, see CharSet.

Another common fixed-size array is the bool array. The elements in a bool array are always one byte in size. bool arrays are not suitable for creating bit arrays or buffers.

Note

Except for memory created with stackalloc, the C# compiler and the common language runtime (CLR) do not perform any security buffer overrun checks. As with all unsafe code, use caution.

Unsafe buffers are different from normal arrays in the following ways:

  • You can only use unsafe buffers in an unsafe context.

  • Unsafe buffers are always vectors, or one-dimensional arrays.

  • The declaration of the array should include a count, such as char id[8]. You cannot use char id[] instead.

  • Unsafe buffers can only be instance fields of structs in an unsafe context.

See Also

Tasks

How to: Call Native DLLs from Managed Code Using PInvoke
How to: Marshal Arrays Using PInvoke
How to: Marshal Embedded Pointers Using PInvoke
How to: Marshal Function Pointers Using PInvoke
How to: Marshal Strings Using PInvoke

Reference

fixed Statement (C# Reference)

Concepts

C# Programming Guide
Unsafe Code and Pointers (C# Programming Guide)
Default Marshaling for Arrays