C# Implicit/Explicit Type Conversion

后端 未结 4 1952
夕颜
夕颜 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:53

    Implicit conversion

    // Implicit conversion. num long can
    // hold any value an int can hold, and more!
    int num = 2147483647;
    long bigNum = num;
    

    Explicit Conversion

    class Test
    {
        static void Main()
        {
            double x = 1234.7;
            int a;
            // Cast double to int.
            a = (int)x;
            System.Console.WriteLine(a);
        }
    }
    

    Hope this may help you.

提交回复
热议问题