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
25 changes: 25 additions & 0 deletions standard/conversions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 10 Conversions

## 10.1 General

Check warning on line 3 in standard/conversions.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 91 > maximum 83; Console.WriteLine(identity.Method.ReturnParameter.IsDefined(typeof(MarkAttribute), false));

Check warning on line 4 in standard/conversions.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 94 > maximum 83; Console.WriteLine(identity.Method.GetParameters()[0].IsDefined(typeof(MarkAttribute), false));
A ***conversion*** causes an expression to be converted to, or treated as being of, a particular type; in the former case a conversion may involve a change in representation. Conversions can be ***implicit*** or ***explicit***, and this determines whether an explicit cast is required.

Expand Down Expand Up @@ -64,6 +64,7 @@
- Default literal conversions ([§10.2.16](conversions.md#10216-default-literal-conversions))
- Implicit throw conversions ([§10.2.17](conversions.md#10217-implicit-throw-conversions))
- Anonymous function type conversions ([§10.2.21](conversions.md#10221-anonymous-function-type-conversion))
- Implicit scan conversions (§implicit-scan-conversions)

Implicit conversions can occur in a variety of situations, including function member invocations ([§12.6.6](expressions.md#1266-function-member-invocation)), cast expressions ([§12.9.8](expressions.md#1298-cast-expressions)), and assignments ([§12.24](expressions.md#1224-assignment-operators)).

Expand Down Expand Up @@ -537,6 +538,18 @@

The conversion of an inline array to a `System.Span` or `System.ReadonlySpan` ignores any declared operators in the inline array type that might otherwise appear to be applicable. See [§16.6](structs.md#166-inline-arrays) for more information.

### §implicit-scan-conversions Implicit scan conversions

An implicit span conversion permits expressions of type *array_type*, `System.Span<T>`, `System.ReadOnlySpan<T>`, and `string` to be converted between each other, as follows:

- From any single-dimensional *array_type* with element type `Eᵢ` to `System.Span<Eᵢ>`
- From any single-dimensional *array_type* with element type `Eᵢ` to `System.ReadOnlySpan<Uᵢ>`, provided that `Eᵢ` is covariance-convertible ([§19.2.3.3](interfaces.md#19233-variance-conversion)) to `Ui`
- From `System.Span<Tᵢ>` to `System.ReadOnlySpan<Uᵢ>`, provided that `Tᵢ` is covariance-convertible to `Uᵢ`
- From `System.ReadOnlySpan<Tᵢ>` to `System.ReadOnlySpan<Uᵢ>`, provided that `Tᵢ` is covariance-convertible to `Uᵢ`
- From `string` to `System.ReadOnlySpan<char>`

Any Span/ReadOnlySpan type is considered applicable for the conversion if it is a ref struct and it matches by its fully-qualified name.

## 10.3 Explicit conversions

### 10.3.1 General
Expand Down Expand Up @@ -763,6 +776,13 @@

A user-defined explicit conversion consists of an optional standard explicit conversion, followed by execution of a user-defined implicit or explicit conversion operator, followed by another optional standard explicit conversion. The exact rules for evaluating user-defined explicit conversions are described in [§10.5.5](conversions.md#1055-user-defined-explicit-conversions).

### §explicit-scan-conversions Explicit span conversions

The explicit span conversions are, as follows:

- All implicit span conversions (§implicit-scan-conversions).
- From an *array_type* with element type `Tᵢ` to `System.Span<Uᵢ>` or `System.ReadOnlySpan<Uᵢ>` provided an explicit reference conversion exists from `Tᵢ` to `Uᵢ`.

## 10.4 Standard conversions

### 10.4.1 General
Expand All @@ -782,6 +802,7 @@
- Implicit constant expression conversions ([§10.2.11](conversions.md#10211-implicit-constant-expression-conversions))
- Implicit conversions involving type parameters ([§10.2.12](conversions.md#10212-implicit-conversions-involving-type-parameters))
- Implicit inline array conversions ([§10.2.23](conversions.md#10223-implicit-inline-array-conversions))
- Implicit scan conversions (§implicit-scan-conversions)

The standard implicit conversions specifically exclude user-defined implicit conversions.

Expand All @@ -791,12 +812,16 @@

> *Note*: In other words, if a standard implicit conversion exists from a type `A` to a type `B`, then a standard explicit conversion exists from type `A` to type `B` and from type `B` to type `A`. *end note*

However, unlike other standard explicit conversions, which always exist given the opposite standard implicit conversion, there is no standard explicit span conversion.

## 10.5 User-defined conversions

### 10.5.1 General

C# allows the pre-defined implicit and explicit conversions to be augmented by user-defined conversions. User-defined conversions are introduced by declaring conversion operators ([§15.10.4](classes.md#15104-conversion-operators)) in class and struct types.

User-defined conversions are not considered when converting between types for which an implicit or an explicit span conversion exists.

### 10.5.2 Permitted user-defined conversions

C# permits only certain user-defined conversions to be declared. In particular, it is not possible to redefine an already existing implicit or explicit conversion.
Expand Down
21 changes: 17 additions & 4 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 @@ -861,6 +861,8 @@
- If `V` is one of the *unfixed* `Xᵢ` then `U` is added to the set of exact bounds for `Xᵢ`.
- Otherwise, sets `V₁...Vₑ` and `U₁...Uₑ` are determined by checking if any of the following cases apply:
- `V` is an array type `V₁[...]` and `U` is an array type `U₁[...]` of the same rank
- `V` is a `Span<V₁>` and `U` is an array type `U₁[]` or a `Span<U₁>`
- `V` is a `ReadOnlySpan<V₁>` and `U` is an array type `U₁[]` or a `Span<U₁>` or `ReadOnlySpan<U₁>`
- `V` is the type `V₁?` and `U` is the type `U₁`
- `V` is a constructed type `C<V₁...Vₑ>` and `U` is a constructed type `C<U₁...Uₑ>`

Expand All @@ -876,6 +878,8 @@
- Otherwise, if `V` is the type `V₁?` and `U` is the type `U₁?` then a lower bound inference is made from `U₁` to `V₁`.
- Otherwise, sets `U₁...Uₑ` and `V₁...Vₑ` are determined by checking if any of the following cases apply:
- `V` is an array type `V₁[...]`and `U` is an array type `U₁[...]`of the same rank
- `V` is a `Span<V₁>` and `U` is an array type `U₁[]` or a `Span<U₁>`
- `V` is a `ReadOnlySpan<V₁>` and `U` is an array type `U₁[]` or a `Span<U₁>` or `ReadOnlySpan<U₁>`
- `V` is one of `IEnumerable<V₁>`, `ICollection<V₁>`, `IReadOnlyList<V₁>>`, `IReadOnlyCollection<V₁>` or `IList<V₁>` and `U` is a single-dimensional array type `U₁[]`
- `V` is a constructed `class`, `struct`, `interface` or `delegate` type `C<V₁...Vₑ>` and there is a unique type `C<U₁...Uₑ>` such that `U` (or, if `U` is a type `parameter`, its effective base class or any member of its effective interface set) is identical to, `inherits` from (directly or indirectly), or implements (directly or indirectly) `C<U₁...Uₑ>`.
- `V` is a function pointer type ([§24.3.3](unsafe-code.md#2433-function-pointers)) `delegate*<V₂..Vₖ, V₁>` and there is a function pointer type `delegate*<U₂..Uₖ, U₁>` such that `U` is identical to `delegate*<U₂..Uₖ, U₁>`, and the calling convention of `V` is identical to `U`, and the refness of `Vᵢ` is identical to `Uᵢ`.
Expand All @@ -884,7 +888,13 @@
If any of these cases apply then an inference is made from each `Uᵢ` to the corresponding `Vᵢ` as follows:
- If `Uᵢ` is not known to be a reference type then an *exact inference* is made; or alternatively, if `U` is not a function pointer type and `Uᵢ` is not known to be a reference type, or if `U` is a function pointer type and `Uᵢ` is not known to be a function pointer type or a reference type, then an exact inference is made
> *Note*: This is only applicable in unsafe code. *end note*
- Otherwise, if `U` is an array type then a *lower-bound inference* is made
- Otherwise, if `U` is an array type then inference depends on the type of `V`:
- If `V` is a `Span<Vᵢ>`, then an *exact inference* is made
- If `V` is an array type or a `ReadOnlySpan<Vᵢ>`, then a *lower-bound inference* is made
- Otherwise, if `U` is a `Span<Uᵢ>` then inference depends on the type of `V`:
- If `V` is a `Span<Vᵢ>`, then an *exact inference* is made
- If `V` is a `ReadOnlySpan<Vᵢ>`, then a *lower-bound inference* is made
- Otherwise, if `U` is a `ReadOnlySpan<Uᵢ>` and `V` is a `ReadOnlySpan<Vᵢ>` a *lower-bound inference* is made
- Otherwise, if `V` is `C<V₁...Vₑ>` then inference depends on the `i-th` type parameter of `C`:
- If it is covariant then a *lower-bound inference* is made.
- If it is contravariant then an *upper-bound inference* is made.
Expand Down Expand Up @@ -1260,8 +1270,9 @@
- `C₁` is not an *anonymous function type conversion* ([§10.2.21](conversions.md#10221-anonymous-function-type-conversion)) and `C₂` is an *anonymous function type conversion*, or
- `E` does not exactly match `T₂` and at least one of the following holds:
- `E` exactly matches `T₁` and `E` does not exactly match `T₂` ([§12.6.4.6](expressions.md#12646-exactly-matching-expression)).
- `E` exactly matches neither of `T₁` and `T₂`, and `C₁` is an implicit span conversion and `C₂` is not an implicit span conversion
- `C₁` is not a conditional expression conversion and `C₂` is a conditional expression conversion.
- `E` exactly matches both or neither of `T₁` and `T₂`, and `T₁` is a better conversion target than `T₂` ([§12.6.4.7](expressions.md#12647-better-conversion-target)) and either `C₁` and `C₂` are both conditional expression conversions or neither is a conditional expression conversion.
- `E` exactly matches both or neither of `T₁` and `T₂`, both or neither of `C₁` and `C₂` are an implicit span conversion, and `T₁` is a better conversion target than `T₂` ([§12.6.4.7](expressions.md#12647-better-conversion-target)) and either `C₁` and `C₂` are both conditional expression conversions or neither is a conditional expression conversion.
- `V` is a function pointer type `delegate*<V₂..Vₖ, V₁>` and `U` is a function pointer type `delegate*<U₂..Uₖ, U₁>`, and the calling convention of `V` is identical to `U`, and the refness of `Vᵢ` is identical to `Uᵢ`.
> *Note*: This is only applicable in unsafe code. *end note*
- `E` is a method group ([§12.2](expressions.md#122-expression-classifications)), `T₁` is compatible ([§21.4](delegates.md#214-delegate-compatibility)) with the single best method from the method group for conversion `C₁`, and `T₂` is not compatible with the single best method from the method group for conversion `C₂`.
Expand Down Expand Up @@ -1339,7 +1350,9 @@

Given two types `T₁` and `T₂`, `T₁` is a ***better conversion target*** than `T₂` if one of the following holds:

- An implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists
- `T₁` is `System.ReadOnlySpan<E₁>`, `T₂` is `System.Span<E₂>`, and an identity conversion from `E₁` to `E₂` exists
- `T₁` is `System.ReadOnlySpan<E₁>`, `T₂` is `System.ReadOnlySpan<E₂>`, and an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists
- At least one of `T₁` or `T₂` is not `System.ReadOnlySpan<Eᵢ>` and is not `System.Span<Eᵢ>`, and an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists
- `T₁` is `«TaskType»<S₁>`([§15.14.1](classes.md#15141-general)), `T₂` is `«TaskType»<S₂>`, and `S₁` is a better conversion target than `S₂`
- `T₁` is `«TaskType»<S₁>`([§15.14.1](classes.md#15141-general)), `T₂` is `«TaskType»<S₂>`, and `T₁` is more specialized than `T₂`
- `T₁` is `S₁` or `S₁?` where `S₁` is a signed integral type, and `T₂` is `S₂` or `S₂?` where `S₂` is an unsigned integral type. Specifically:
Expand Down Expand Up @@ -2417,7 +2430,7 @@
- `Cᵢ` is a non-generic, non-nested class
- The name of `Mₑ` is *identifier*
- `Mₑ` is accessible and applicable when applied to the arguments as a static method as shown above
- An implicit identity, reference or boxing conversion exists from *expr* to the type of the first parameter of `Mₑ`.
- An implicit identity, reference, boxing, or span conversion exists from *expr* to the type of the first parameter of `Mₑ`. Span conversion is not considered when overload resolution is performed for a method group conversion.

The search for `C` proceeds as follows:

Expand Down
Loading