By 22/07/2015 12 Comments

C# Implicit Casting

One type of data is assign to another type variable, an implicit type conversion will occur automatically by C# compiler when the following conditions are met:

  • The two data types must be compatible with each other (Implicit or automatic casting program example are early explained in Example-1).
  • The destination (this is a type variable which holds the converted value in it) data type has a greater or equal range than the source type (this is the value of a data type’s variable which will be converted and assigned to a destination variable).

Example, implicit or automatic casting / type conversion:

using System;
namespace csharpBasic
  // Start class definition / declaration.
    class Program
      // Static main method void type declaration.
        static void Main(string[] args)
         // Declare an int data type variable.
            int i;

         // Declare a long data type variable.
            long l;

         // Assign 500 value to int type i variable.
            i = 500;

         // Assign an int data type to a long data type, this is ok.
            l = i;

         // Declare a byte data type variable.
            byte b;

         // Declare a short data type variable.     
            short s;

         // Assign 50 value to byte type b variable.
            b = 50;

         // Assign a byte data type to a short data type, this is ok.
            s = b;

         // Declare a float data type variable.
            float f;

         // Declare a double data type variable.
            double d;

         // Assign 1.25f value to float type f variable.
            f = 1.25f;

         // Assign a float data type to a double data type, this is ok.
            d = f;

           // Print long variable.
            Console.WriteLine("long: {0}", l);

           // Print short variable.
            Console.WriteLine("short: {0}", s);

           // print double variable.
            Console.WriteLine("double: {0}", d);

        } // End of main method definition.
    } // End of class.

Next Tutorial →

← Previous Tutorial

Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .