|
1 | 1 | ---
|
2 | 2 | title: "Fixed Size Buffers - C# Programming Guide"
|
3 |
| -ms.date: 04/20/2018 |
4 |
| -helpviewer_keywords: |
| 3 | +ms.date: 04/23/2020 |
| 4 | +helpviewer_keywords: |
5 | 5 | - "fixed size buffers [C#]"
|
6 | 6 | - "unsafe buffers [C#]"
|
7 | 7 | - "unsafe code [C#], fixed size buffers"
|
@@ -32,15 +32,39 @@ The preceding example demonstrates accessing `fixed` fields without pinning, wh
|
32 | 32 |
|
33 | 33 | Another common fixed-size array is the [bool](../../language-reference/builtin-types/bool.md) array. The elements in a `bool` array are always one byte in size. `bool` arrays are not appropriate for creating bit arrays or buffers.
|
34 | 34 |
|
35 |
| -> [!NOTE] |
36 |
| -> Except for memory created by using [stackalloc](../../language-reference/operators/stackalloc.md), the C# compiler and the common language runtime (CLR) do not perform any security buffer overrun checks. As with all unsafe code, use caution. |
| 35 | +Fixed size buffers are compiled with the <xref:System.Runtime.CompilerServices.UnsafeValueTypeAttribute?displayProperty=nameWithType>, which instructs the common language runtime (CLR) that a type contains an unmanaged array that can potentially overflow. This is similar to memory created using [stackalloc](../../language-reference/operators/stackalloc.md), which automatically enables buffer overrun detection features in the CLR. The previous example shows how a fixed size buffer could exist in an `unsafe struct`. |
37 | 36 |
|
38 |
| -Unsafe buffers differ from regular arrays in the following ways: |
| 37 | +```csharp |
| 38 | +internal unsafe struct Buffer |
| 39 | +{ |
| 40 | + public fixed char fixedBuffer[128]; |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +The compiler generated C# for `Buffer`, is attributed as follows: |
| 45 | + |
| 46 | +```csharp |
| 47 | +internal struct Buffer |
| 48 | +{ |
| 49 | + [StructLayout(LayoutKind.Sequential, Size = 256)] |
| 50 | + [CompilerGenerated] |
| 51 | + [UnsafeValueType] |
| 52 | + public struct <fixedBuffer>e__FixedBuffer |
| 53 | + { |
| 54 | + public char FixedElementField; |
| 55 | + } |
| 56 | + |
| 57 | + [FixedBuffer(typeof(char), 128)] |
| 58 | + public <fixedBuffer>e__FixedBuffer fixedBuffer; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Fixed size buffers differ from regular arrays in the following ways: |
39 | 63 |
|
40 |
| -- You can only use unsafe buffers in an unsafe context. |
41 |
| -- Unsafe buffers are always vectors, or one-dimensional arrays. |
42 |
| -- The declaration of the array should include a count, such as `char id[8]`. You cannot use `char id[]`. |
43 |
| -- Unsafe buffers can only be instance fields of structs in an unsafe context. |
| 64 | +- May only be used in an [unsafe](../../language-reference/keywords/unsafe.md) context. |
| 65 | +- May only be instance fields of structs. |
| 66 | +- They're always vectors, or one-dimensional arrays. |
| 67 | +- The declaration should include the length, such as `fixed char id[8]`. You cannot use `fixed char id[]`. |
44 | 68 |
|
45 | 69 | ## See also
|
46 | 70 |
|
|
0 commit comments