From a23bd55f3c081e4f086909cca3dd8b1741f55f67 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 3 Oct 2024 12:51:33 -0400 Subject: [PATCH] Remove explicit dynamic conversions (#1184) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove §10.3.8 on explicit dynamic conversions. §10.2.10 already defines these conversions as *implicit* dynamic conversions. Therefore, none of these conversions would apply. Remove references to the removed section. --- standard/conversions.md | 44 +---------------------------------------- 1 file changed, 1 insertion(+), 43 deletions(-) diff --git a/standard/conversions.md b/standard/conversions.md index 2af5d9733..d886586be 100644 --- a/standard/conversions.md +++ b/standard/conversions.md @@ -72,7 +72,7 @@ The pre-defined implicit conversions always succeed and never cause exceptions t For the purposes of conversion, the types `object` and `dynamic` are identity convertible ([§10.2.2](conversions.md#1022-identity-conversion)). -However, dynamic conversions ([§10.2.10](conversions.md#10210-implicit-dynamic-conversions) and [§10.3.8](conversions.md#1038-explicit-dynamic-conversions)) apply only to expressions of type `dynamic` ([§8.2.4](types.md#824-the-dynamic-type)). +However, dynamic conversions ([§10.2.10](conversions.md#10210-implicit-dynamic-conversions)) apply only to expressions of type `dynamic` ([§8.2.4](types.md#824-the-dynamic-type)). ### 10.2.2 Identity conversion @@ -394,7 +394,6 @@ The following conversions are classified as explicit conversions: - Explicit interface conversions - Unboxing conversions ([§10.3.7](conversions.md#1037-unboxing-conversions)) - Explicit type parameter conversions ([§10.3.9](conversions.md#1039-explicit-conversions-involving-type-parameters)) -- Explicit dynamic conversions ([§10.3.8](conversions.md#1038-explicit-dynamic-conversions)) - User-defined explicit conversions ([§10.3.10](conversions.md#10310-user-defined-explicit-conversions)) Explicit conversions can occur in cast expressions ([§12.9.7](expressions.md#1297-cast-expressions)). @@ -539,47 +538,6 @@ For an unboxing conversion to a given *non_nullable_value_type* to succeed at ru For an unboxing conversion to a given *nullable_value_type* to succeed at run-time, the value of the source operand shall be either null or a reference to a boxed value of the underlying *non_nullable_value_type* of the *nullable_value_type*. If the source operand is a reference to an incompatible object, a `System.InvalidCastException` is thrown. -### 10.3.8 Explicit dynamic conversions - -An explicit dynamic conversion exists from an expression of type `dynamic` to any type `T`. The conversion is dynamically bound ([§12.3.3](expressions.md#1233-dynamic-binding)), which means that an explicit conversion will be sought at run-time from the run-time type of the expression to `T`. If no conversion is found, a run-time exception is thrown. - -If dynamic binding of the conversion is not desired, the expression can be first converted to `object`, and then to the desired type. - -> *Example*: Assume the following class is defined: -> -> -> -> ```csharp -> class C -> { -> int i; -> -> public C(int i) -> { -> this.i = i; -> } -> -> public static explicit operator C(string s) -> { -> return new C(int.Parse(s)); -> } -> } -> ``` -> -> The following illustrates explicit dynamic conversions: -> -> -> ```csharp -> object o = "1"; -> dynamic d = "2"; -> var c1 = (C)o; // Compiles, but explicit reference conversion fails -> var c2 = (C)d; // Compiles and user defined conversion succeeds -> ``` -> -> The best conversion of `o` to `C` is found at compile-time to be an explicit reference conversion. This fails at run-time, because `"1"` is not in fact a `C`. The conversion of `d` to `C` however, as an explicit dynamic conversion, is suspended to run-time, where a user defined conversion from the run-time type of `d` (`string`) to `C` is found, and succeeds. -> -> *end example* - ### 10.3.9 Explicit conversions involving type parameters For a *type_parameter* `T` that is known to be a reference type ([§15.2.5](classes.md#1525-type-parameter-constraints)), the following explicit reference conversions ([§10.3.5](conversions.md#1035-explicit-reference-conversions)) exist: