Span/ReadOnlySpan fundamentals (C# 6 emulation)
Emulate Span/ReadOnlySpan with ArraySegment : create views over arrays, slice with offset/length, and process data without copying.
Span/ReadOnlySpan fundamentals (C# 6 emulation) is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why slices matter
Aim: Work with slices without extra allocations.
- Use ArraySegment<T> as a view
- Slice by offset and count
- Write methods that accept ArraySegment<T>
- Keep operations simple and safe
Zero-copy view
ArraySegment<T> is a zero-copy view over part of an array. Pass it to methods to avoid slicing copies.
using System;
public class Program
{
static int Sum(ArraySegment<int> seg)
{
int acc = 0;
int end = seg.Offset + seg.Count;
for (int i = seg.Offset; i < end; i++) acc += seg.Array[i];
return acc;
}
public static void Main(string[] args)
{
int[] data = new int[] { 2, 4, 6, 8, 10 };
ArraySegment<int> middle3 = new ArraySegment<int>(data, 1, 3); // 4,6,8
Console.WriteLine("Sum = " + Sum(middle3)); // 18
}
}
Nested slices
You can build helper Slice to take a subrange of an existing segment. Still zero-copy.
using System;
public static class Seg
{
public static ArraySegment<T> Slice<T>(ArraySegment<T> seg, int start, int count)
{
if (start < 0 || count < 0 || start + count > seg.Count) throw new ArgumentOutOfRangeException();
return new ArraySegment<T>(seg.Array, seg.Offset + start, count);
}
}
public class Program
{
public static void Main(string[] args)
{
string[] words = new string[] { "alpha", "beta", "gamma", "delta", "epsilon" };
ArraySegment<string> mid = new ArraySegment<string>(words, 1, 3); // beta, gamma, delta
ArraySegment<string> inner = Seg.Slice<string>(mid, 1, 1); // gamma
Console.WriteLine(inner.Array[inner.Offset]); // gamma
}
}
Operate in-place by view
Operate by offset/count to avoid creating substrings; this mirrors ReadOnlySpan-based trimming.
using System;
public static class SegOps
{
public static ArraySegment<char> Trim(ArraySegment<char> seg)
{
int start = seg.Offset;
int end = seg.Offset + seg.Count - 1;
while (start <= end && Char.IsWhiteSpace(seg.Array[start])) start++;
while (end >= start && Char.IsWhiteSpace(seg.Array[end])) end--;
int newCount = (end >= start) ? (end - start + 1) : 0;
return new ArraySegment<char>(seg.Array, start, newCount);
}
}
public class Program
{
public static void Main(string[] args)
{
char[] buf = " hello world ".ToCharArray(); // single allocation for chars
ArraySegment<char> all = new ArraySegment<char>(buf, 0, buf.Length);
ArraySegment<char> trimmed = SegOps.Trim(all);
// Print the trimmed part without new string:
for (int i = 0; i < trimmed.Count; i++)
Console.Write(trimmed.Array[trimmed.Offset + i]);
Console.WriteLine();
}
}
Materialize on demand
Sometimes you need a real copy. Materialize the segment into a new array only at the boundary.
using System;
public static class SegCopy
{
public static T[] ToArray<T>(ArraySegment<T> seg)
{
T[] copy = new T[seg.Count];
Array.Copy(seg.Array, seg.Offset, copy, 0, seg.Count);
return copy;
}
}
public class Program
{
public static void Main(string[] args)
{
int[] data = new int[] { 1, 2, 3, 4, 5, 6 };
ArraySegment<int> view = new ArraySegment<int>(data, 2, 3); // 3,4,5
int[] materialized = SegCopy.ToArray<int>(view); // copy only when you must
Console.WriteLine(string.Join(",", materialized));
}
}
Tips & limits
Tips:
- Design APIs to accept ArraySegment<T> when you work on arrays.
- Keep track of offset and count.
- Copy only at boundaries (I/O, interop).
- Modern Span offers safety and stack views; here we use segments to learn the slicing mindset.
Slice emulation in C# 6
Recap
Recap: Use ArraySegment<T> to view subranges, write APIs that accept segments, operate via offset/count, and copy only when necessary.
Frequently asked questions
Is the “Span/ReadOnlySpan fundamentals (C# 6 emulation)” lesson free?
Yes — the full text of “Span/ReadOnlySpan fundamentals (C# 6 emulation)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Span/ReadOnlySpan fundamentals (C# 6 emulation)”?
Emulate Span/ReadOnlySpan with ArraySegment : create views over arrays, slice with offset/length, and process data without copying. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Span/ReadOnlySpan fundamentals (C# 6 emulation)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Span/ReadOnlySpan fundamentals (C# 6 emulation)
- Memory/ReadOnlyMemory emulation: zero-copy windows
- String-as-Span APIs (C# 6 emulation)