Casting from Object types within native types - Exception case in C#

The examples show how C# behaves strictly towards the object type conversions.


Example 1:
Normal conversion from int to long

using System;
using System.Text;

namespace forgetCode
{
    class program
    {
        public static void Main()
        {
            
            int  value1 = 124;
            long value2 = (long)value1;
            Console.WriteLine(value2);
        }
    }
}


Output:
124


Example 2:
The following code throws exception even if casting is done between native types and with wide casting.

using System;
using System.Text;

namespace forgetCode
{
    class program
    {
        public static void Main()
        {
            // Assign integer and then cast it to an object implictly.
            int  value1 = 124;
            object value2 = value1;

            // Explicitly cast to long again from the object type.
            //unProper casting
            long value3 = (long)value2;
            Console.WriteLine(value3);

        }
    }
}


Output:

Unhandled Exception: System.InvalidCastException: Specified cast is not valid at forgetCode.program.Main()