Created
May 7, 2021 09:25
-
-
Save MichalBrylka/78541b3822f5d19261902cc96931975b to your computer and use it in GitHub Desktop.
Play with unsafe string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
namespace SafeString | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var arr = PlaySafe("Hello World!"); | |
Console.WriteLine($"LENGTH = {arr.Length}"); | |
Console.WriteLine($"'{new string(arr)}'"); | |
Console.WriteLine("START"); | |
foreach (char a in arr) | |
Console.WriteLine(a); | |
Console.WriteLine("END"); | |
} | |
private static char[] PlaySafe(string helloWorld) => Unsafe.As<string, char[]>(ref helloWorld); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment