Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use a fixed statement

Shulou Source: shulou.com Published: 2022-06-03 06:09:27 11月16日 Update

This article introduces the knowledge of "how to use fixed statements". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The variable in the fixed statement is a read-only variable. If you need to modify the value of this variable, redeclare a pointer variable, and then modify the corresponding value of the variable. If you want to allocate memory in the fixed statement, it will not be affected by GC. Use stackalloc expressions (see https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/stackalloc for stackalloc expressions

The stackalloc expression allocates blocks of memory on the stack. Memory allocated using stackalloc cannot be explicitly freed, and memory blocks allocated in the stack are not affected by GC and do not need to be fixed by fixed statements. Stackalloc can be used for system.Span ReadOnlySpan or pointer types. For pointer types, they can only be used in the context of unsafe. Span does not need it, and it is officially recommended to use Span instead of pointer types.

Unsafe

{

Int length=3

Int* numbers = stackallock int [length]

For (var iTuno Bandi)

Tags: Statement variable memory pointer type expression content stack more knowledge middle score allocation influence utility achievement next up and down context dilemma official Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi MySQL Xiaomi Docker