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
11 changes: 11 additions & 0 deletions standard/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ A number of attributes affect the language in some way. These attributes include
- `System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute` and `System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute`, which are used to declare a custom interpolated string expression handler ([§23.5.11.1](attributes.md#235111-custom-interpolated-string-expression-handlers)) and to call one of its constructors, respectively.
- System.Diagnostics.CodeAnalysis.UnscopedRefAttribute ([§23.5.8](attributes.md#2358-the-unscopedref-attribute)), which allows an otherwise implicitly scoped ref to be treated as not being scoped.
- `System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute` ([§23.5.12.1](attributes.md#235121-the-setsrequiredmembers-attribute)) and `System.Runtime.CompilerServices.RequiredMemberAttribute` ([§23.5.12.2](attributes.md#235122-the-requiredmember-attribute)), which are used in required-member contexts ([§15.7.1](classes.md#1571-general)).
- `System.Runtime.CompilerServices.CollectionBuilderAttribute` (§collection-builder-attr), which designates a collection type as having a collection-creation method.

The Nullable static analysis attributes ([§23.5.7](attributes.md#2357-code-analysis-attributes)) can improve the correctness of warnings generated for nullabilities and null states ([§8.9.5](types.md#895-nullabilities-and-null-states)).

Expand Down Expand Up @@ -1739,6 +1740,16 @@ This attribute indicates that the constructor it decorates sets all required mem

This attribute indicates that the current type has one or more required members ([§15.7.1](classes.md#1571-general)), or that a specific member of that type is required. However, it is an error for this attribute to be used explicitly. Instead, the presence of the modifier `required` results in the type or member being treated as if it were decorated with this attribute.

### §collection-builder-attr The CollectionBuilder attribute

This attribute designates a collection type as having a collection-creation method (§declaring-a-collection-type-general).

The constructor takes a builder type and the name of the method to be invoked to construct an instance of the collection type.

The attribute can be applied to a class, struct, ref struct, or interface. The attribute is not inherited although it can be applied to a base class or an abstract class.

The builder type shall be a non-generic class or struct.

## 23.6 Attributes for interoperation

For interoperation with other languages, an indexer may be implemented using indexed properties. If the indexer is not decorated with `IndexerNameAttribute`, then the name `Item` is used by default. `IndexerNameAttribute` enables a developer to override this default and specify a different name.
Expand Down
173 changes: 172 additions & 1 deletion standard/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,8 @@ Except for allowing a variable number of arguments in an invocation, a parameter
> F(arr);
> F(10, 20, 30, 40);
> F();
> F([-5, 3, 9]);
> F([]);
> }
> }
> ```
Expand All @@ -2582,6 +2584,8 @@ Except for allowing a variable number of arguments in an invocation, a parameter
> Array contains 3 elements: 1 2 3
> Array contains 4 elements: 10 20 30 40
> Array contains 0 elements:
> Array contains 3 elements: -5 3 9
> Array contains 0 elements:
> ```
>
> The first invocation of `F` simply passes the array `arr` as a value parameter. The second invocation of F automatically creates a four-element `int[]` with the given element values and passes that array instance as a value parameter. Likewise, the third invocation of `F` creates a zero-element `int[]` and passes that instance as a value parameter. The second and third invocations are precisely equivalent to writing:
Expand All @@ -2591,7 +2595,7 @@ Except for allowing a variable number of arguments in an invocation, a parameter
> F(new int[] {});
> ```
>
> *end example*
> The fourth and fifth invocations pass a three-element and an empty collection expression, respectively. *end example*

When performing overload resolution, a method with a parameter array might be applicable, either in its normal form or in its expanded form ([§12.6.4.2](expressions.md#12642-applicable-function-member)). The expanded form of a method is available only if the normal form of the method is not applicable and only if an applicable method with the same signature as the expanded form is not already declared in the same type.

Expand Down Expand Up @@ -6925,6 +6929,173 @@ A positional record class ([§15.2.1](classes.md#1521-general)) with at least on
>
> *end example*

## §declaring-a-collection-type Declaring a collection type

### §declaring-a-collection-type-general General

There are a number of contexts in which a collection expression (§collection-expressions) may be converted to a collection type (§imp-collection-expression-conv). One of them is for a target class, struct, or interface type to be made a collection type by annotating it with an attribute, as shown below.

Here is a simple user-defined collection type and its associated builder type:

<!-- Example: {template:"standalone-lib", name:"CollectionType1"} -->
```csharp
[CollectionBuilder(typeof(MyCollectionBuilder), "Create")]
public class MyCollection<T> : IEnumerable<T>
{
private readonly T[] _storage;
public int Count { get; }
public T this[int index]
{
get
{
return _storage[index];
}

set
{
_storage[index] = value;
}
}
public MyCollection(ReadOnlySpan<T> elements)
{
Count = elements.Length;
_storage = new T[Count];
for (int i = 0; i < Count; i++)
{
_storage[i] = elements[i];
}
}
public IEnumerator<T> GetEnumerator() =>
((IEnumerable<T>)_storage).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
_storage.GetEnumerator();
}
internal static class MyCollectionBuilder
{
internal static MyCollection<T>
Create<T>(ReadOnlySpan<T> values) =>
new MyCollection<T>(values);
}
```

The collection type shall be annotated with `CollectionBuilderAttribute` (§collection-builder-attr) that designates an associated, non-generic builder class or struct type having a collection-creation method (whose name is user-defined; in this case, it is `Create`).

The job of a ***collection-creation method*** is to create and initialize an instance of its associated collection type.

First, the set of applicable collection-creation methods `CM` is determined. It consists of methods that meet the following requirements:

- Be directly declared in the builder type
- Have the name designated by `CollectionBuilderAttribute`
- Be static
- Be accessible at its point of use
- Its arity shall match that of the collection type
- It shall have a single parameter of type `System.ReadOnlySpan<E>`, passed by value.
- There shall be an implicit identity conversion ([§10.2.2](conversions.md#1022-identity-conversion)), an implicit reference conversion ([§10.2.8](conversions.md#1028-implicit-reference-conversions)), or a boxing conversion ([§10.2.9](conversions.md#1029-boxing-conversions)) from the method return type to the collection type.

Methods declared on base types or interfaces are ignored and not part of the `CM` set.

If the `CM` set is empty, then the collection type doesn't have an element type, doesn't have a collection-creation method, and none of the following steps apply.

If only one method among those in the `CM` set has an identity conversion from `E` to the element type of the collection type, that is the collection-creation method for the collection type. Otherwise, the collection type doesn't have a collection-creation method.

It is an error if `CollectionBuilderAttribute` does not refer to an invokable method with the expected signature.

For a *collection_expression* with a target type `C<S₀, S₁, …>` where the type declaration `C<T₀, T₁, …>` has an associated collection-creation method `B.M<U₀, U₁, …>()`, the generic type arguments from the target type are applied in order (from outermost containing type to innermost) to the collection-creation method.

The span parameter for the collection-creation method may be explicitly marked `scoped` or `[UnscopedRef] ([§9.7.3](variables.md#973-the-scoped-modifier))`. If the parameter is implicitly or explicitly `scoped`, the compiler may allocate the storage for the span on the stack rather than the heap.

The construction of an instance of a collection type is described in §collection-construction.

### §collection-construction Collection construction

The *collection_element*s of a *collection_expression* are evaluated in order, left to right. Each *collection_element* is evaluated exactly once, and any further references to the any elements refer to the results of this initial evaluation.

A *spread_element* may be iterated before or after the subsequent elements in the *collection_expression* are evaluated. Spread enumerations may be reordered under the well-behaved collection assumptions below. The resulting element sequence retains the order of the *collection_element*s and, within each spread, the order of enumeration.

An unhandled exception thrown from any of the methods used during construction shall go uncaught and shall prevent further steps in the construction.

Collections used in collection expressions are assumed to be ***well-behaved***:

- `Length` and `Count`, when used to determine the number of elements, agree with the number of elements produced by enumeration.
- `Length`, `Count`, and `GetEnumerator` are assumed to have no side effects, and spread enumeration is assumed to have no side effects for the purpose of reordering enumerations.
- An applicable `AddRange` operation produces the same final collection values as enumerating its argument and invoking the applicable `Add` method for each item, in order.
- An applicable `CopyTo` operation produces the same element values, in the same order, as enumeration and individual addition or assignment to the destination.

The construction steps may use these equivalences to optimize construction. The behavior of collection expressions using collections that are not well-behaved is undefined. These assumptions do not change the required left-to-right evaluation of element expressions or the disposal requirements below. Methods described as applicable are subject to ordinary member lookup, accessibility, and overload resolution.

If the target type is a struct or class type that implements `System.Collections.IEnumerable`, and the target type does not have a collection-creation method (§declaring-a-collection-type-general), the construction of the collection instance steps are, as follows:

- The elements are evaluated in order. Some or all elements may be evaluated during the steps below rather than before.
- The compiler may determine the known length of the collection expression by invoking countable properties ([§18.1](ranges.md#181-general)) or equivalent properties from well-known interfaces or types, on each *spread_element*’s *expression*.
- The constructor that is applicable with no arguments is invoked.
- For each *collection_element*, in order:

- If the *collection_element* is an *expression_element*, the applicable `Add` instance or extension method is invoked with the *element_expression* as the argument. (Unlike classic collection initializer behavior ([§12.8.17.2.3](expressions.md#1281723-collection-initializers)), element evaluation and `Add` calls are not necessarily interleaved.)
- If the *collection_element* is a *spread_element* then one of the following steps is used:

- An applicable `GetEnumerator` instance or extension method is invoked on the *spread_element*’s *expression*, and for each item from the enumerator the applicable `Add` instance or extension method is invoked on the collection instance with the item as the argument. If the enumerator implements `IDisposable`, then `Dispose` shall be called after enumeration, regardless of any exceptions.
- An applicable `AddRange` instance or extension method is invoked on the collection instance with the *spread_element*’s *expression* as the argument.
- An applicable `CopyTo` instance or extension method is invoked on the *spread_element*’s *expression* with the collection instance and `int` index as arguments.

- During the construction steps above, an applicable `EnsureCapacity` instance or extension method may be invoked one or more times on the collection instance with an `int` capacity argument.

If the target type is an array, a `Span` or `ReadOnlySpan`, a type with a collection-creation method, or an interface, the construction steps of the collection instance are, as follows:

- The elements are evaluated in order. Some or all elements may be evaluated during the steps below rather than before.
- The compiler may determine the known length of the collection expression by invoking countable properties (or equivalent properties from well-known interfaces or types) on each *spread_element*’s *expression*.

- An *initialization instance* is created as follows:

- If the target type is an array and the collection expression has a known length, an array is allocated with the expected length.
- If the target type is a `Span` or `ReadOnlySpan`, or a type with a collection-creation method, and the collection has a known length, a `Span` or `ReadOnlySpan` with the expected length is created referring to contiguous storage.
- Otherwise, intermediate storage is allocated.

- For each *collection_element*, in order:

- If the *collection_element*is an *expression_element*, the initialization instance indexer is invoked to add the evaluated expression at the current index.
- If the element is a *spread_element* then one of the following is used:

- A member of a well-known interface or type is invoked to copy items from the spread element expression to the initialization instance.
- An applicable `GetEnumerator` instance or extension method is invoked on the *spread_element*’s *expression*, and for each item from the enumerator, the initialization instance indexer is invoked to add the item at the current index. If the enumerator implements `IDisposable`, then `Dispose` shall be called after enumeration, regardless of any exceptions.
- An applicable `CopyTo` instance or extension method is invoked on the *spread_element*’s *expression* with the initialization instance and `int` index as arguments.

- For an array or span target, if intermediate storage was allocated, the result contains the actual collection length and the values from the initialization instance. A span may refer to the appropriate portion of the intermediate storage. Otherwise, the initialization instance is the result.
- If the target type has a collection-creation method, that method is invoked with the span instance, and the result of the invocation is converted to the target type. The method shall return a collection that preserves the element values and their order from the span argument. Its storage or other representation shall keep those values safely available for the lifetime permitted by the result's safe-context (§collection-expressions), without depending on storage whose lifetime has ended. This is a behavioral obligation of the collection-creation method, not a condition used to determine its applicability. It does not require a physical copy of the span's storage.
- For one of the five generic interface targets specified in §imp-collection-expression-conv, a result satisfying the interface contract below is produced from the element values. The initialization instance need not itself implement the target interface.

For a target type of `System.Collections.Generic.ICollection<T>` or `System.Collections.Generic.IList<T>`, the result has the observable collection behavior of `System.Collections.Generic.List<T>`. It supports mutation through the generic and non-generic collection interfaces, with `ICollection<T>.IsReadOnly`, `System.Collections.IList.IsReadOnly`, and `System.Collections.IList.IsFixedSize` returning `false`. Mutating one result shall not change the contents of another independently constructed result.

For a target type of `System.Collections.Generic.IEnumerable<T>`, `System.Collections.Generic.IReadOnlyCollection<T>`, or `System.Collections.Generic.IReadOnlyList<T>`, the result implements the target interface and the non-generic `System.Collections.ICollection` and `System.Collections.IList` interfaces. It may implement additional interfaces. `System.Collections.IList.IsReadOnly` and `System.Collections.IList.IsFixedSize` return `true`; `ICollection<T>.IsReadOnly` also returns `true` if that interface is implemented. Calls to mutation members of any implemented collection interface throw an exception.

Interface results preserve the element values and their sequence, and their storage or other representation keeps those values available after construction, independently of the lifetime of intermediate storage or subsequent changes to spread-source collections. No particular implementing type or storage representation is required.

An empty collection expression may use shared empty storage or an existing empty result, and storage allocation may be omitted, provided the result satisfies the requirements of its target type and required expression evaluation and the observable effects of construction methods are preserved. In particular, empty mutable interface results shall permit independent mutation; a shared immutable result cannot satisfy that contract. No result identity or sharing is guaranteed. The safe-context of an empty collection expression is specified in §collection-expressions.

> *Note:* The compiler might delay adding elements to the collection (or delay iterating through *spread_element*s) until after evaluating subsequent elements. (When subsequent *spread_element*s have countable properties that would allow calculating the expected length of the collection before allocating the collection.) Conversely, the compiler might eagerly add elements to the collection (and eagerly iterate through *spread_element*s) when there is no advantage to delaying.
>
> Consider the following collection expression:
>
> ```csharp
> int[] x = [a, ..b, ..c, d];
> ```
>
> If *spread_element*s `b` and `c` are countable, the compiler could delay adding items from `a` and `b` until after `c` is evaluated, to allow allocating the resulting array at the expected length. After that, the compiler could eagerly add items from `c`, before evaluating `d`, as shown below.
>
> ```csharp
> var __tmp1 = a;
> var __tmp2 = b;
> var __tmp3 = c;
> var __result = new int[2 + __tmp2.Length + __tmp3.Length];
> int __index = 0;
> __result[__index++] = __tmp1;
> foreach (var __i in __tmp2) __result[__index++] = __i;
> foreach (var __i in __tmp3) __result[__index++] = __i;
> __result[__index++] = d;
> x = __result;
> ```
>
> *end note*

## 15.17 Record class and non-record class differences

A record class differs from a non-record class in several important ways:
Expand Down
Loading
Loading