By 22/07/2015 9 Comments

C# Type Conversion Or Casting

  • When we assign one type of a variable to another type of variable that process is called type conversion.

Example Р1, assign a long data type value into a double data type variable:

using System;
namespace csharpBasic
{
  // Start class definition / declaration.
    class Program
    {
      // Static main method void type declaration.
        static void Main(string[] args)
        {
         // Declare a long type variable.
            long l;

         // Declare a double type variable.
            double d;

         // Assign 500 value to long type variable.
            l = 500;

         // Assign long data type to a double data type. This is ok.
            d = l;

         // Print double type.
            Console.WriteLine("double: {0}", d);
            Console.ReadKey();
        } // End of main method definition.
    } // End of class.
}

  • Notice above examples shows us that both long and double data types are compatible types.¬†The value of the right side is automatically converted to the left side type, Notice in above example the right side value is a long data type’s value which is 500 and it is assigned to the left side data type which is a double data type, remember the value of l converted first into double data type and then assigned to d.
  • Above program is performing an implicitly or automatic type conversion because these both (long) and (double) data types are compatible with each other. C# is a strong type language so all data types are not compatible with each other, for instance we cannot assign or convert an int data type into a bool or vice versa implicitly.

Example – 2, Assign an int value into a bool data type variable:

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 bool data type variable.
            bool b;

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

         // Assign int i data type to a bool b data type, not ok.          
            b = i; // Compile-time error!

         // Bool type variable will not be printed due to compile-error.
            Console.WriteLine("double: {0}", b);
            Console.ReadKey();

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

Types of casting:

  • Implicit casting.
  • Explicit casting.





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 .