Implicit and Explicit Conversions
Posted by Gizmosis350k
Last Updated: April 19, 2012
  2023

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch05Ex01
{
   class Program
   {
      static void Main(string[] args)
      {
         short shortResult, shortVal = 4;
         int integerVal = 67;
         long longResult;
         float floatVal = 10.5F;
         double doubleResult, doubleVal = 99.999;
         string stringResult, stringVal = "17";
         bool boolVal = true;

         Console.WriteLine("Variable Conversion Examples\n");

         doubleResult = floatVal * shortVal;
         Console.WriteLine("Implicit, -> double: {0} * {1} -> {2}", floatVal,
            shortVal, doubleResult);

         shortResult = (short)floatVal;
         Console.WriteLine("Explicit, -> short:  {0} -> {1}", floatVal,
            shortResult);

         stringResult = Convert.ToString(boolVal) +
            Convert.ToString(doubleVal);
         Console.WriteLine("Explicit, -> string: \"{0}\" + \"{1}\" -> {2}",
            boolVal, doubleVal, stringResult);

         longResult = integerVal + Convert.ToInt64(stringVal);
         Console.WriteLine("Mixed,    -> long:   {0} + {1} -> {2}",
            integerVal, stringVal, longResult);
         Console.ReadKey();

      }
   }
}



This code shows some examples of implicit and explicit conversion, as well as displaying some examples of mixing various variable types. It was simple enough until i encountered the .NET versions of the casts and such, but i will be working through these in sequence as they pop up.
   
  
 
 
   

 
 

Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

int num = 2147483647;

long bigNum = num;

 

Explicit conversions (casts): Explicit conversions require a cast operator. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons.  Typical examples include numeric conversion to a type that has less precision or a smaller range, and conversion of a base-class instance to a derived class.

double x = 1234.7;
int a;
a = (int)x;
System.Console.WriteLine(a);

____________________________________________
GREAT WORK MAN!!!
0_0
 
LOL


O_O!
 
COOOLLL.....!!!!!
 
Awesome.

And these conversions are again very important in the basic scheme of mathematical calculations in programs.
Feel free to add another example involving an int variable and a short variable.
I'd be glad to see it.
o_o
 
class Animal
{
    public void Eat() { Console.WriteLine("Eating."); }
    public override string ToString()
    {
        return "I am an animal.";
    }
}
class Reptile : Animal { }
class Mammal : Animal { }

class UnSafeCast
{
    static void Main()
    {            
        Test(new Mammal());

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }

    static void Test(Animal a)
    {
        // Cause InvalidCastException at run time 
        // because Mammal is not convertible to Reptile.
        Reptile r = (Reptile)a;
    }

}