Skip to content

Commit ddca34c

Browse files
authored
Clarify index and range usages (#46496)
Fixes #44907 Indexes and ranges are only usable with single dimensional arrays, not multi-dimensional arrays.
1 parent f33513f commit ddca34c

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

docs/csharp/language-reference/operators/member-access-operators.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Member access and null-conditional operators and expressions:"
33
description: "C# operators that you use to access type members or null-conditionally access type members. These operators include the dot operator - `.`, indexers - `[`, `]`, `^` and `..`, and invocation - `(`, `)`."
4-
ms.date: 04/04/2025
4+
ms.date: 05/29/2025
55
author: pkulikov
66
f1_keywords:
77
- "._CSharpKeyword"
@@ -35,7 +35,7 @@ helpviewer_keywords:
3535
---
3636
# Member access operators and expressions - the dot, indexer, and invocation operators.
3737

38-
You use several operators and expressions to access a type member. Member access operators include member access (`.`), array element, or indexer access (`[]`), index-from-end (`^`), range (`..`), null-conditional operators (`?.` and `?[]`), and method invocation (`()`). These include the *null-conditional* member access (`?.`), and indexer access (`?[]`) operators.
38+
You use several operators and expressions to access a type member. Member access operators include member access (`.`), array element, or indexer access (`[]`), index-from-end (`^`), range (`..`), null-conditional operators (`?.` and `?[]`), and method invocation (`()`). These operators include the *null-conditional* member access (`?.`), and indexer access (`?[]`) operators.
3939

4040
- [`.` (member access)](#member-access-expression-): to access a member of a namespace or a type
4141
- [`[]` (array element or indexer access)](#indexer-operator-): to access an array element or a type indexer
@@ -205,6 +205,9 @@ You also use parentheses to adjust the order in which to evaluate operations in
205205

206206
Index and range operators can be used with a type that is *countable*. A *countable* type is a type that has an `int` property named either `Count` or `Length` with an accessible `get` accessor. [Collection expressions](./collection-expressions.md) also rely on *countable* types.
207207

208+
> [!NOTE]
209+
> Single dimensional arrays are *countable*. Multi-dimensional arrays aren't. The `^` and `..` (range) operators can't be used in multi-dimensional arrays.
210+
208211
The `^` operator indicates the element position from the end of a sequence. For a sequence of length `length`, `^n` points to the element with offset `length - n` from the start of a sequence. For example, `^1` points to the last element of a sequence and `^length` points to the first element of a sequence.
209212

210213
:::code language="csharp" source="snippets/shared/MemberAccessOperators.cs" id="IndexFromEnd":::

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: What's new in C# 13
33
description: Get an overview of the new features in C# 13.
4-
ms.date: 01/27/2025
4+
ms.date: 05/29/2025
55
ms.topic: whats-new
66
---
77
# What's new in C# 13
@@ -71,7 +71,7 @@ You can read the details of the changes in the [proposal specification](~/_cshar
7171

7272
## Implicit index access
7373

74-
The implicit "from the end" index operator, `^`, is now allowed in an object initializer expression. For example, you can now initialize an array in an object initializer as shown in the following code:
74+
The implicit "from the end" index operator, `^`, is now allowed in an object initializer expression for single-dimension collections. For example, you can now initialize a single-dimension array using an object initializer as shown in the following code:
7575

7676
```csharp
7777
public class TimerRemaining
@@ -103,7 +103,7 @@ In versions before C# 13, the `^` operator can't be used in an object initialize
103103

104104
## `ref` and `unsafe` in iterators and `async` methods
105105

106-
This feature and the following two features enable `ref struct` types to use new constructs. You won't use these unless you write your own `ref struct` types. More likely, you'll see an indirect benefit as <xref:System.Span`1?displayProperty=nameWithType> and <xref:System.ReadOnlySpan`1?displayProperty=nameWithType> gain more functionality.
106+
This feature and the following two features enable `ref struct` types to use new constructs. You won't use these features unless you write your own `ref struct` types. More likely, you'll see an indirect benefit as <xref:System.Span`1?displayProperty=nameWithType> and <xref:System.ReadOnlySpan`1?displayProperty=nameWithType> gain more functionality.
107107

108108
Before C# 13, iterator methods (methods that use `yield return`) and `async` methods couldn't declare local `ref` variables, nor could they have an `unsafe` context.
109109

0 commit comments

Comments
 (0)