Saturday 23 December 2017

Different type of conversions


  1.  Implicit Conversions:
    - No special syntax is required because the conversion is type safe and no data will be lost.
    - eg: Conversion from smaller to larger integral types, and conversion from derived classes to base classes.
  2.  Explicit Conversions (Casts):
    - Explicit conversion requires a cast operator.
    - Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons.
    - eg: Numeric conversion to a type that has less precision or a small range, and conversion of a base class instance to a derived class.
  3.  User-defined Conversions:
    - Performed by special methods that you can define to enable explicit and implicit conversions between custom types that do not have a base class- derived class relationship.
  4.  Conversion with helper classes:
    - To convert between non-compatible types, such as integers and system datetime objects, or hexadecimal strings and byte arrays, you can use the system.bitconverter class, the system.convert class, and the parse method of the built-in numeric types, such as Int32.Parse.

No comments:

Post a Comment