Created
July 3, 2018 02:38
-
-
Save Sharpiro/6361f02af85ce318a69c1cc7916a8aae to your computer and use it in GitHub Desktop.
Span
This file contains 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; | |
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