Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created July 8, 2019 04:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cosminpopescu14/f992c2c0f0010a45f0454fcff5ac1e23 to your computer and use it in GitHub Desktop.
from Microsoft docs
char[] chars = { 'w', 'o', 'r', 'd' };
sbyte[] bytes = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x00 };
// Create a string from a character array.
string string1 = new string(chars);
Console.WriteLine(string1);
// Create a string that consists of a character repeated 20 times.
string string2 = new string('c', 20);
Console.WriteLine(string2);
string stringFromBytes = null;
string stringFromChars = null;
unsafe
{
fixed (sbyte* pbytes = bytes)
{
// Create a string from a pointer to a signed byte array.
stringFromBytes = new string(pbytes);
}
fixed (char* pchars = chars)
{
// Create a string from a pointer to a character array.
stringFromChars = new string(pchars);
}
}
Console.WriteLine(stringFromBytes);
Console.WriteLine(stringFromChars);
// The example displays the following output:
// word
// cccccccccccccccccccc
// ABCDE
// word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment