Skip to content

Commit 5bfbbe2

Browse files
authored
Create a tutorial for C# 8 syntax for indices and ranges (#11944)
* add first draft * edits to first draft. * Work through the samples and proofread. * add a link from "What's new" * use "indices" as the standard term. * update based on feedback.
1 parent 3169f3c commit 5bfbbe2

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

docs/csharp/tutorials/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ on your machine.
6363
* [Nullable reference types](nullable-reference-types.md): demonstrates how to use nullable reference types to express your intent for null references.
6464
* [Update a project to make use of Nullable reference types](upgrade-to-nullable-references.md): demonstrates techniques to upgrade an existing project to make use of nullable reference types.
6565
* [Extend data capabilities using pattern matching](pattern-matching.md): demonstrates how to use pattern matching to extend types beyond their core features.
66+
* [Work with data sequences using indexes and ranges](ranges-indexes.md): Demonstrates new convenient syntax for accessing single elements or ranges of a sequential data container.
6667

6768
## General Tutorials
6869

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Explore ranges of data using indices and ranges
3+
description: This advanced tutorial teaches you to explore data using indices and ranges to examine slices of a sequential data set.
4+
ms.date: 04/19/2019
5+
ms.custom: mvc
6+
---
7+
# Indices and ranges
8+
9+
Ranges and indices provide a succinct syntax for accessing single elements or ranges in an <xref:System.Array>, <xref:System.Span%601>, or <xref:System.ReadOnlySpan%601>. These features enable more concise, clear syntax to access single elements or ranges of elements in a sequence.
10+
11+
In this tutorial, you'll learn how to:
12+
13+
> [!div class="checklist"]
14+
> * Use the syntax for ranges in a sequence.
15+
> * Understand the design decisions for the start and end of each sequence.
16+
> * Learn scenarios for the <xref:System.Index> and <xref:System.Range> types.
17+
18+
## Language support for indices and ranges
19+
20+
You can specify an index **from the end**. You specify **from the end** using the `^` operator. You're familiar with `array[2]` meaning the element "2 from the start". Now, `array[^2]` means the element "2 from the end". The index `^0` means "the end", or the index that follows the last element.
21+
22+
You can specify a **range** with the **range operator**: `..`. For example, `0..^0` specifies the entire range of the array: 0 from the start up to, but not including 0 from the end. Either operand may use "from the start" or "from the end". Furthermore, either operand may be omitted. The defaults are `0` for the start index, and `^0` for the end index.
23+
24+
```csharp-interactive
25+
string[] words = new string[]
26+
{
27+
// index from start index from end
28+
"The", // 0 ^9
29+
"quick", // 1 ^8
30+
"brown", // 2 ^7
31+
"fox", // 3 ^6
32+
"jumped", // 4 ^5
33+
"over", // 5 ^4
34+
"the", // 6 ^3
35+
"lazy", // 7 ^2
36+
"dog" // 8 ^1
37+
};
38+
```
39+
40+
The index of each element reinforces the concept of "from the start", and "from the end", and that ranges are exclusive of the end of the range. The "start" of the entire array is the first element. The "end" of the entire array is *past* the last element.
41+
42+
You can retrieve the last word with the `^1` index. Add the following code below the initialization:
43+
44+
[!code-csharp[LastIndex](~/samples/csharp/tutorials/RangesIndexes/IndicesAndRanges.cs#IndicesAndRanges_LastIndex)]
45+
46+
The following code creates a subrange with the words "quick", "brown", and "fox". It includes `words[1]` through `words[3]`. The element `words[4]` isn't in the range. Add the following code to the same method. Copy and paste it at the bottom of the interactive window.
47+
48+
[!code-csharp[Range](~/samples/csharp/tutorials/RangesIndexes/IndicesAndRanges.cs#IndicesAndRanges_Range)]
49+
50+
The following code creates a subrange with "lazy" and "dog". It includes `words[^2]` and `words[^1]`. The end index `words[^0]` isn't included. Add the following code as well:
51+
52+
[!code-csharp[LastRange](~/samples/csharp/tutorials/RangesIndexes/IndicesAndRanges.cs#IndicesAndRanges_LastRange)]
53+
54+
The following examples create ranges that are open ended for the start, end, or both:
55+
56+
[!code-csharp[PartialRange](~/samples/csharp/tutorials/RangesIndexes/IndicesAndRanges.cs#IndicesAndRanges_PartialRanges)]
57+
58+
You can also declare ranges or indices as variables. The variable can then be used inside the `[` and `]` characters:
59+
60+
[!code-csharp[IndexRangeTypes](~/samples/csharp/tutorials/RangesIndexes/IndicesAndRanges.cs#IndicesAndRanges_RangeIndexTypes)]
61+
62+
The previous examples show two design decisions that require more explanation:
63+
64+
- Ranges are *exclusive*, meaning the element at the last index isn't in the range.
65+
- The index `^0` is *the end* of the collection, not *the last element* in the collection.
66+
67+
The following sample shows many of the reasons for those choices. Modify `x`, `y`, and `z` to try different combinations. When you experiment, use values where `x` is less than `y`, and `y` is less than `z` for valid combinations. Add the following code in a new method. Try different combinations:
68+
69+
[!code-csharp[SemanticsExamples](~/samples/csharp/tutorials/RangesIndexes/IndicesAndRanges.cs#IndicesAndRanges_Semantics)]
70+
71+
## Scenarios for Indices and Ranges
72+
73+
You'll often use ranges and indices when you want to perform some analysis on subrange of an entire sequence. The new syntax is clearer in reading exactly what subrange is involved. The local function `MovingAverage` takes a <xref:System.Range> as its argument. The method then enumerates just that range when calculating the min, max, and average. Try the following code in your project:
74+
75+
[!code-csharp[MovingAverages](~/samples/csharp/tutorials/RangesIndexes/IndicesAndRanges.cs#IndicesAndRanges_MovingAverage)]
76+
77+
You can download the completed code from the [dotnet/samples](https://github.com/dotnet/samples/tree/master/csharp/tutorials/RangesIndexes) repository.

docs/csharp/whats-new/csharp-8.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,5 @@ The range can then be used inside the `[` and `]` characters:
377377
```csharp
378378
var text = words[phrase];
379379
```
380+
381+
You can explore more about indices and ranges in the tutorial on [indices and ranges](../tutorials/ranges-indexes.md).

docs/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,8 @@
533533
href: csharp/tutorials/exploration/interpolated-strings-local.md
534534
- name: Advanced scenarios for string Interpolation
535535
href: csharp/tutorials/string-interpolation.md
536+
- name: Explore indexes and ranges
537+
href: csharp/tutorials/ranges-indexes.md
536538
- name: Work with nullable reference types
537539
href: csharp/tutorials/nullable-reference-types.md
538540
- name: Upgrade an app to nullable reference types

0 commit comments

Comments
 (0)