string myString = "Hello World!"; ReadOnlySpanreadOnlySpan = myString.AsReadOnlySpan(); Console.WriteLine(readOnlySpan.ToString()); // Prints Hello World!
string myString = "This is a test string"; ReadOnlySpanreadOnlySpan = myString.AsReadOnlySpan(10, 4); // Start at index 10, end at index 14 (4 positions) Console.WriteLine(readOnlySpan.ToString()); // Prints test
string myString1 = "hello"; string myString2 = "hell"; ReadOnlySpanThese examples are all part of the System.Runtime.CompilerServices package library and require that package to be installed before using them.span1 = myString1.AsReadOnlySpan(); ReadOnlySpan span2 = myString2.AsReadOnlySpan(); bool isEqual = span1.SequenceEqual(span2); // isEqual would be false Console.WriteLine(isEqual);