C# Implicit/Explicit Type Conversion

后端 未结 4 1925
夕颜
夕颜 2021-01-18 00:36

I have a simple scenario that may or may not be possible. I have a class that contains an integer, for this purpose I\'ll make it as simple as possible:

pub         


        
4条回答
  •  攒了一身酷
    2021-01-18 00:45

    Implicit type conversion: Implicit type conversion takes place between smaller to larger integral types but not vice-versa or between derived class and base class. Conversion takes place in type safe manner by C# and no data loss takes place. For example,

    int a = 10;
    long b = a;
    float f = a;
    

    Explicit type conversion: Explicit type conversion done by using built-in C# functions. Data may be lost while explicit type conversion, means if we convert double to int then precision may be lost. Explicit type conversion require casting. To do a casting, need to specify the type that you are casting to in front of the value or variable to be converted.

    For example,

    double d = 10.20;
    int a = (int)d;
    //Output: 10
    

    To understand in details follow C# Basics - C# Type Conversion

提交回复
热议问题