Zarat
Zarat

Reputation: 2754

Copy string to memory buffer in C#

What is the best way to copy a string into a raw memory buffer in C#?

Note that I already know how and when to use String and StringBuilder, so don't suggest that ;) - I'm going to need some text processing & rendering code and currently it looks like a memory buffer is both easier to code and more performant, as long as I can get the data into it. (I'm thinking of B-tree editor buffers and memory mapped files, something which doesn't map well into managed C# objects but is easily coded with pointers.)

Things I already considered:

Upvotes: 3

Views: 1768

Answers (1)

CodesInChaos
CodesInChaos

Reputation: 108810

enable unsafe code(Somewhere in the project options), then use:

unsafe
{
    fixed(char* pc = myString)
    {
    }
}

and then just use low level memory copies.

Upvotes: 4

Related Questions