// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Linq; namespace SpanFun { public class Program { private static long GetAddress(int[] a) { unsafe { fixed (int* p = &a[0]) { return (long)p; } } } private static long GetAddress(Span s) { unsafe { fixed (int* p = &s[0]) { return (long)p; } } } private static void Dump(string lbl, int[] a) { Console.WriteLine("{0} : Address = {1}, Length = {2}", lbl, GetAddress(a), a.Length); } private static void Dump(string lbl, Span s) { Console.WriteLine("{0} : Address = {1}, Length = {2}", lbl, GetAddress(s), s.Length); } public static void Main(string[] args) { int[] a = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; Dump("a ", a); Span s1 = a[1..3]; Dump("s1", s1); Span s2 = ((Span)a).Slice(3, 2); Dump("s2", s2); Span s3 = (new Span(a)).Slice(5, 2); Dump("s3", s3); Span s4 = new Span(a, 7, 2); Dump("s4", s4); } } }