Home
C#
Array Slice
Updated Oct 25, 2022
Dot Net Perls
Array slice. A slice of an array is a range of elements. By using extension methods and generics, we simplify and clarify array slices.
Extension benefits. In the C# language, extension methods make for more reusable and powerful code. We write and test an array slice method.
Array
Input and output. Consider an array of integers. We wish to take a slice of a certain range of integers. The first argument is the start index, and the second is the last index (exclusive).
Slice(2, 5): 1 2 3 4 5 6 7
Example. Here we see a generic method, which lets it act on any type. Our Slice method here uses this ability to simulate the built-in slice functionality of Python and other languages.
Detail This class contains a public static method called Slice. The Slice method is an extension method.
Extension
Also The other confusing part is the use of the letter T. The Type T is replaced by the C# compiler with any type you specify.
using System; using System.Collections.Generic; class Program { static void Main() { int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7 }; foreach (int i in a.Slice(0, 1)) { Console.WriteLine(i); } Console.WriteLine(); foreach (int i in a.Slice(0, 2)) { Console.WriteLine(i); } Console.WriteLine(); foreach (int i in a.Slice(2, 5)) { Console.WriteLine(i); } Console.WriteLine(); foreach (int i in a.Slice(2, -3)) { Console.WriteLine(i); } } } public static class Extensions { /// <summary> /// Get the array slice between the two indexes. /// ... Inclusive for start index, exclusive for end index. /// </summary> public static T[] Slice<T>(this T[] source, int start, int end) { // Handles negative ends. if (end < 0) { end = source.Length + end; } int len = end - start; // Return new array. T[] res = new T[len]; for (int i = 0; i < len; i++) { res[i] = source[i + start]; } return res; } }
1 1 2 3 4 5 3 4
Notes, generics. When T (or a similar letter) appears in brackets after a method name, this is a generic method. The letter is replaced with the type specified.
Generic
A discussion. How is this method different from LINQ Take and Skip? The method here can deal with negative parameters. And it may be faster because it doesn't use IEnumerable.
Take, TakeWhile
Skip, SkipWhile
Detail In my development efforts, I have also used string slice methods, which are proven equivalent in the main aspects to JavaScript.
String Slice
A summary. Here we saw a generic array slice method that is also an extension method. An extension method is appropriate as the method can be used throughout a project.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Oct 25, 2022 (simplify).
Home
Changes
© 2007-2025 Sam Allen