Skip to content

Instantly share code, notes, and snippets.

@Sharpiro
Created July 3, 2018 02:38
Show Gist options
  • Save Sharpiro/6361f02af85ce318a69c1cc7916a8aae to your computer and use it in GitHub Desktop.
Save Sharpiro/6361f02af85ce318a69c1cc7916a8aae to your computer and use it in GitHub Desktop.
Span
using System;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
var stringData = "hello Steve";
var dangerousSpan = stringData.AsSpan();
dangerousSpan[6] = 'M';
WriteWord(dangerousSpan);
Console.WriteLine(stringData);
}
public static void WriteWord(ReadOnlySpan<char> word)
{
for (var i = 0; i < word.Length; i++)
{
Console.Write(word[i]);
}
Console.WriteLine();
}
}
public static class Extension
{
public static Span<char> AsSpan(this string data)
{
var readonlySpan = data.AsReadOnlySpan();
ref char dataStartRef = ref readonlySpan.DangerousGetPinnableReference();
var dangerousSpan = Span<char>.DangerousCreate(data, ref dataStartRef, data.Length);
return dangerousSpan;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment