IT. Expert System.

C#

Implicit conversions


The following conversions are classified as implicit conversions:

  • Identity conversions

  • Implicit numeric conversions

  • Implicit enumeration conversions.

  • Implicit nullable conversions

  • Null literal conversions

  • Implicit reference conversions

  • Boxing conversions

  • Implicit constant expression conversions

  • User-defined implicit conversions

  • Anonymous function conversions

  • Method group conversions

Implicit conversions can occur in a variety of situations, including function member invocations , cast expressions , and assignments .

The pre-defined implicit conversions always succeed and never cause exceptions to be thrown. Properly designed user-defined implicit conversions should exhibit these characteristics as well.

      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.

      1. Implicit numeric conversions

The implicit numeric conversions are:

  • From sbyte to short, int, long, float, double, or decimal.

  • From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.

  • From short to int, long, float, double, or decimal.

  • From ushort to int, uint, long, ulong, float, double, or decimal.

  • From int to long, float, double, or decimal.

  • From uint to long, ulong, float, double, or decimal.

  • From long to float, double, or decimal.

  • From ulong to float, double, or decimal.

  • From char to ushort, int, uint, long, ulong, float, double, or decimal.

  • From float to double.

Conversions from int, uint, long, or ulong to float and from long or ulong to double may cause a loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never lose any information.

There are no implicit conversions to the char type, so values of the other integral types do not automatically convert to the char type.

      1. Implicit enumeration conversions

An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type and to any nullable-type whose underlying type is an enum-type. In the latter case the conversion is evaluated by converting to the underlying enum-type and wrapping the result .

      1. Implicit nullable conversions

Predefined implicit conversions that operate on non-nullable value types can also be used with nullable forms of those types. For each of the predefined implicit identity and numeric conversions that convert from a non-nullable value type S to a non-nullable value type T, the following implicit nullable conversions exist:

  • An implicit conversion from S? to T?.

  • An implicit conversion from S to T?.

Evaluation of an implicit nullable conversion based on an underlying conversion from S to T proceeds as follows:

  • If the nullable conversion is from S? to T?:

  • If the source value is null (HasValue property is false), the result is the null value of type T?.

  • Otherwise, the conversion is evaluated as an unwrapping from S? to S, followed by the underlying conversion from S to T, followed by a wrapping from T to T?.

  • If the nullable conversion is from S to T?, the conversion is evaluated as the underlying conversion from S to T followed by a wrapping from T to T?.

      1. Null literal conversions

An implicit conversion exists from the null literal to any nullable type. This conversion produces the null value of the given nullable type.

      1. 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 a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces, provided that there is an implicit identity or reference conversion from S to T.

  • From any delegate-type to System.Delegate.

  • From the null literal to any reference-type.

  • Implicit conversions involving type parameters that are known to be reference types. See §6.1.9 for more details on implicit conversions involving type parameters.

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.

Unlike array types, constructed reference types do not exhibit “covariant” conversions. This means that a type List<B> has no conversion (either implicit or explicit) to List<A> even if B is derived from A. Likewise, no conversion exists from List<B> to List<object>.

      1. Boxing conversions

A boxing conversion permits a value-type to be implicitly converted to a reference type. A boxing conversion exists from any non-nullable-value-type to object, to System.ValueType and to any interface-type implemented by the non-nullable-value-type. Furthermore an enum-type can be converted to the type System.Enum.

A boxing conversion exists from a nullable-type to a reference type, if and only if a boxing conversion exists from the underlying non-nullable-value-type to the reference type.

Boxing a value of a non-nullable-value-type consists of allocating an object instance and copying the value-type value into that instance. A struct can be boxed to the type System.ValueType, since that is a base class for all structs .

Boxing a value of a nullable-type proceeds as follows:

  • If the source value is null (HasValue property is false), the result is a null reference of the target type.

  • Otherwise, the result is a reference to a boxed T produced by unwrapping and boxing the source value.

Boxing conversions are described further in §4.3.1.

      1. Implicit constant expression conversions

An implicit constant expression conversion permits the following conversions:

  • A constant-expression of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.

  • A constant-expression of type long can be converted to type ulong, provided the value of the constant-expression is not negative.

      1. Implicit conversions involving type parameters

The following implicit conversions exist for a given type parameter T:

  • From T to its effective base class C, from T to any base class of C, and from T to any interface implemented by C. At run-time, if T is a value type, the conversion is executed as a boxing conversion. Otherwise, the conversion is executed as an implicit reference conversion or identity conversion.

  • From T to an interface type I in T’s effective interface set and from T to any base interface of I. At run-time, if T is a value type, the conversion is executed as a boxing conversion. Otherwise, the conversion is executed as an implicit reference conversion or identity conversion.

  • From T to a type parameter U, provided T depends on U. At run-time, if T is a value type and U is a reference type, the conversion is executed as a boxing conversion. Otherwise, if both T and U are value types, then T and U are necessarily the same type and no conversion is performed. Otherwise, if T is a reference type, then U is necessarily also a reference type and the conversion is executed as an implicit reference conversion or identity conversion.

  • From the null literal to T, provided T is known to be a reference type.

If T is known to be a reference type , the conversions above are all classified as implicit reference conversions . If T is not known to be a reference type, the conversions described in the first two bullets above are classified as boxing conversions .

      1. User-defined implicit conversions

A user-defined implicit conversion consists of an optional standard implicit conversion, followed by execution of a user-defined implicit conversion operator, followed by another optional standard implicit conversion. The exact rules for evaluating user-defined implicit conversions are described in §6.4.4.

      1. Anonymous function conversions and method group conversions

Anonymous functions and method groups do not have types in and of themselves, but may be implicitly converted to delegate types or expression tree types. Anonymous function conversions are described in more detail in §6.5 and method group conversions in §6.6.



Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 85 / 158779934. Delta: 0.04182 с