Вопрос

I'm reading through the generics section currently and have come across a section titled "Conversion Type Constraints". I couldn't get my head around Jon's explanation so I typed the term into Google to find no results that use this exact phrase nor explain it in more detail.

Does anyone have any websites/pages which describe "Conversion Type Constraints" in more detail? Or perhaps someone knows of a more widely used term to describe them?

The description given is: A constraint that lets you specify another type that the type argument must be implicitly convertible to via an identity, reference or boxing conversion

Это было полезно?

Решение

Yes, this isn't an official term - as far as I can tell, there is no one official term for the three similar constraints listed as class-type, interface-type or type-parameter in the constructions shown in section 10.1.5 of the C# specification.

It's a shame there isn't an official term for this set of constraints, as they're clearly closely related - they're all used to constrain the type argument such that there is a reference conversion or identity conversion from the type argument to the specified constraint type.

Другие советы

I think that Jon only enumerates the supported constraints. Below you may find a short description for the identity, reference and boxing conversion, as they are described on the MSDN C# Language Specification.

6.1.1 Identity conversion

An identity conversion converts from any type to the same type. This conversion exists only such that an entity that already has a required type can be said to be convertible to that type.

6.1.4 Implicit reference conversions

The implicit reference conversions are:

  • From any reference-type to object.
  • From any class-type S to any class-type T, provided S is derived from T.
  • From any class-type S to any interface-type T, provided S implements T.
  • From any interface-type S to any interface-type T, provided S is derived from T.
  • From an array-type S with an element type SE to an array-type T with an element type TE, provided all of the following are true:
    • S and T differ only in element type. In other words, S and T have the same number of dimensions.
    • Both SE and TE are reference-types.
    • An implicit reference conversion exists from SE to TE.
  • From any array-type to System.Array.
  • From any delegate-type to System.Delegate.
  • From the null type to any reference-type.

The implicit reference conversions are those conversions between reference-types that can be proven to always succeed, and therefore require no checks at run-time. Reference conversions, implicit or explicit, never change the referential identity of the object being converted. In other words, while a reference conversion may change the type of the reference, it never changes the type or value of the object being referred to.

6.1.5 Boxing conversions

A boxing conversion permits a value-type to be implicitly converted to a reference-type. Boxing a value of a value-type consists of allocating an object instance and copying the value-type value into that instance.

See also 4.3.1 Boxing conversions.

There is a dedicated chapter to conversions in the Standard ECMA-334, C# Language specification (see 13. Conversions)

I think that Jon just uses his own term for type parameter constraints that, like Tim already stated, are well documented. Maybe he was misled by the (possibly not perfectly correct) error message "The type 'x' must be convertible to 'y' in order to use it as parameter T ..." that the compiler reports in case the type constraints include a class or interface and the passed type is not derived from that class respectively does not implement the interface.

The big question here is, what "convertible" means. The documentation for the type parameter constraints does only say that in case the constraint is a class or an interface the type must be derived from that class or implement the interface. Passing a type that is not derived or does not implement the interface results in the error message above.

Even if a type that is not derived or does not implement the interface implements an implicit conversion operator that allows a conversion, it cannot be used on the type parameter (to be sure I tried that).

Another possible "conversion" does not come into my mind. If a class is derived from another class or implements an interface referencing instances using the base type or the interface is not really a conversion, but polymorphism.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top