Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions standard/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6492,6 +6492,7 @@ extension_body
extension_member_declaration
: method_declaration
| property_declaration
| indexer_declaration
| operator_declaration
;
```
Expand Down Expand Up @@ -6664,6 +6665,29 @@ An original-style extension method can be rewritten in the new style.

An extension property may not have an `init` accessor.

### §Extension-indexers Extension indexers

> *Example*: Here is an extension indexer declaration:
>
> <!-- Example: {template:"standalone-lib", name:"ExtensionMembersDecls4", replaceEllipsis:true, customEllipsisReplacements: ["true", "new string(\"\")"]} -->
> ```csharp
> static class BitExtensions
> {
> extension<T>(T t)
> {
> public bool this[int index]
> {
> get => ...;
> set => ...;
> }
> }
> }
> ```
>
> *end example*

As an indexer is required to be an instance member, an extension block that declares an indexer shall provide a named receiver parameter.

### 15.14.4 Extension operators

> *Example*: Here is an extension operator declaration:
Expand Down
31 changes: 30 additions & 1 deletion standard/documentation-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,36 @@ Although developers are free to create their own set of tags, a recommended set

- The `<param>` tag is used to describe parameters. If such a tag is used, the documentation generator must verify that the specified parameter exists and that all parameters are described in documentation comments. If such verification fails, the documentation generator issues a warning.

- The `cref` attribute can be attached to any tag to provide a reference to a code element. The documentation generator must verify that this code element exists. If the verification fails, the documentation generator issues a warning. When looking for a name described in a `cref` attribute, the documentation generator must respect namespace visibility according to using statements appearing within the source code. For code elements that are generic, the normal generic syntax (e.g., “`List<T>`”) cannot be used because it produces invalid XML. Braces can be used instead of brackets (e.g.; “`List{T}`”), or the XML escape syntax can be used (e.g., “`List&lt;T&gt;`”). A code element that is an extension member within an extension block needs special notation. For example, an extension method `M` in an extension block `extension(int value)` inside type `T` must be referenced using `T.extension(int).M` (or `T.extension(System.Int32).M`).
- The `cref` attribute can be attached to any tag to provide a reference to a code element. The documentation generator must verify that this code element exists. If the verification fails, the documentation generator issues a warning. When looking for a name described in a `cref` attribute, the documentation generator must respect namespace visibility according to using statements appearing within the source code. For code elements that are generic, the normal generic syntax (e.g., “`List<T>`”) cannot be used because it produces invalid XML. Braces can be used instead of brackets (e.g.; “`List{T}`”), or the XML escape syntax can be used (e.g., “`List&lt;T&gt;`”). A code element that is an extension member within an extension block needs special notation. For example:

> *Example*: Here is an extension indexer declaration:
>
> <!-- Example: {template:"standalone-lib", name:"ExtensionMembersCrefs", replaceEllipsis:true, customEllipsisReplacements: ["0", "new string(\"\")"]} -->
> ```csharp
> /// <see cref="E.extension(int).this[string]"/>
> /// <see cref="E.extension(int).get_Item(string)"/>
> /// <see cref="E.extension(int).get_Item"/>
> /// <see cref="E.extension(int).set_Item(string, int)"/>
> /// <see cref="E.extension(int).set_Item"/>
> /// <see cref="E.get_Item(int, string)"/>
> /// <see cref="E.get_Item"/>
> /// <see cref="E.set_Item(int, string, int)"/>
> /// <see cref="E.set_Item"/>
> public static class E
> {
> extension(int i)
> {
> /// <summary></summary>
> public int this[string s]
> {
> get => ...;
> set => ...;
> }
> }
> }
> ```
>
> *end example*

- The `<summary>` tag is intended to be used by a documentation viewer to display additional information about a type or member.
- The `<include>` tag includes information from an external XML file.
Expand Down
46 changes: 44 additions & 2 deletions standard/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

### 12.2.1 General

The result of an expression is classified as one of the following:

Check warning on line 18 in standard/expressions.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 99 > maximum 83; Console.WriteLine(formattable.ToString(System.Globalization.CultureInfo.InvariantCulture));

- A value. Every value has an associated type.
- A variable. Unless otherwise specified, a variable is explicitly typed and has an associated type, namely the declared type of the variable. An implicitly typed variable has no associated type.
Expand Down Expand Up @@ -2894,7 +2894,9 @@

#### 12.8.12.5 Indexer access

For an indexer access, the *primary_expression* of the *element_access* shall be a variable or value of a class, struct, or interface type, and this type shall implement one or more indexers that are applicable with respect to the *argument_list* of the *element_access*. The *argument_list* shall not contain `out` or `ref` arguments.
##### §indexer-access-general General

For an indexer access, the *primary_expression* of the *element_access* shall be a variable or value of a class, struct, or interface type, and this type shall implement, or have as an extension, one or more indexers that are applicable with respect to the *argument_list* of the *element_access*. The *argument_list* shall not contain `out` or `ref` arguments.

The binding-time processing of an indexer access of the form `P[A]`, where `P` is a *primary_expression* of a class, struct, or interface type `T`, and `A` is an *argument_list*, consists of the following steps:

Expand All @@ -2903,7 +2905,7 @@
- If `I` is not applicable with respect to `A` ([§12.6.4.2](expressions.md#12642-applicable-function-member)), then `I` is removed from the set.
- If `I` is applicable with respect to `A` ([§12.6.4.2](expressions.md#12642-applicable-function-member)), then all indexers declared in a base type of `S` are removed from the set.
- If `I` is applicable with respect to `A` ([§12.6.4.2](expressions.md#12642-applicable-function-member)) and `S` is a class type other than `object`, all indexers declared in an interface are removed from the set.
- If the resulting set of candidate indexers is empty, then no applicable indexers exist, and a binding-time error occurs.
- If the resulting set of candidate indexers is empty, an attempt is made to process the construct as an extension indexer access as described in §extension-indexer-access. If no applicable indexers exist a binding-time error occurs.
- The best indexer of the set of candidate indexers is identified using the overload resolution rules of [§12.6.4](expressions.md#1264-overload-resolution). If a single best indexer cannot be identified, the indexer access is ambiguous, and a binding-time error occurs.
- The accessors of the best indexer are checked:
- If the indexer access is the target of an assignment then the indexer shall have a set or ref get accessor, otherwise, a binding-time error occurs;
Expand All @@ -2924,6 +2926,46 @@
- If the indexer access is the target of an assignment, the set accessor or ref get accessor is invoked to assign a new value ([§12.24.2](expressions.md#12242-simple-assignment)).
- In all other cases, the get accessor or ref get accessor is invoked to obtain the current value ([§12.2.2](expressions.md#1222-values-of-expressions)).

##### §extension-indexer-access Extension indexer access

If the normal processing of an indexer access (§indexer-access-general) finds no applicable indexer, an attempt is made to process the construct as an extension indexer access, as follows.

1. Attempt to bind using the instance indexers declared (or inherited) on the receiver type. If an applicable candidate is found, overload resolution can proceed.
1. Otherwise, attempt to bind using the implicit instance indexers declared (or inherited) on the receiver type. If an applicable candidate is found, overload resolution can proceed.
1. Otherwise, attempt to bind as an extension indexer access. If this process finds an applicable candidate, overload resolution can proceed.
1. Otherwise, attempt to bind as an extension implicit indexer access. If this process finds an applicable candidate, overload resolution can proceed

Extension indexers are not considered when the receiver is a *base_access*.

> *Note*: An *element_access* is only processed as an indexer access if the receiver
is a variable or value, so extension indexers are never considered when the
receiver is a type. *end note*

Given an *element_access* `E[A]`, the objective is to identify an extension indexer.

A candidate extension indexer is applicable with respect to receiver `E` and argument list `A` if an expanded signature, comprised of the type parameters of the extension block and a parameter list combining the extension parameter with the indexer's parameters, is applicable with respect to an argument list combining the receiver `E` with the argument list `A`.

The same scopes examined for extension method invocation are traversed, including the current and enclosing lexical scopes and `using` namespace or `using static` imports.

Considering each scope in turn:

- Extension blocks in non-generic static class declarations in the current scope are considered.
- The indexers in those extension blocks comprise the candidate set.
- Candidates that are not accessible are removed from the set.
- Candidates that are not applicable (as defined above) are removed from the set.
- If the resulting set of candidate indexers is empty, then processing moves to the next scope. If the end of the final scope is reached without finding any candidates, an attempt is made to see if there is an extension implicit indexer.
- Overload resolution is applied to the candidate set. If a single best indexer cannot be identified, the extension indexer access is ambiguous, and a compile-time error occurs.

Using this single best indexer, the indexer access is then processed as a static method invocation.

Depending on the context in which it is used, an indexer access causes invocation of either the get accessor or the set accessor of the indexer. If the indexer access is the target of an assignment, the set accessor static implementation method is invoked to assign a new value. In all other cases, the get accessor static implementation method is invoked to obtain the current value. Either way, the invocation uses generic arguments inferred during the applicability check and the receiver as the first argument.

An extension implicit `System.Index` (or `System.Range`) indexer access is applicable if:

1. The *element_access* has a single argument that is of type `System.Index` (or `System.Range`), and
1. An applicable `Length` or `Count` (instance or extension) property is found on the receiver type, and
1. An applicable `this[int]` (or `Slice(int, int)`) (instance or extension) indexer is found on the receiver type.

### 12.8.13 Null Conditional Element Access

A *null_conditional_element_access* consists of a *primary_expression* followed by the two tokens “`?`” and “`[`”, followed by an *argument_list*, followed by a “`]`” token, followed by zero or more *dependent_access*es any of which can be preceded by a *null_forgiving_operator*.
Expand Down
Loading