Skip to content

Instantly share code, notes, and snippets.

@MichalBrylka
Created May 7, 2021 09:25
Show Gist options
  • Save MichalBrylka/78541b3822f5d19261902cc96931975b to your computer and use it in GitHub Desktop.
Save MichalBrylka/78541b3822f5d19261902cc96931975b to your computer and use it in GitHub Desktop.
Play with unsafe string
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