By 22/07/2015 10 Comments

C# Explicit Casting

  • Explicit casting is an instruction, C# gives that instruction to compiler to convert a data type / expression into a specific type.
  • In above program an int is assigned to a long data type, byte is assigned to short and float is assigned to double data type, that is done by an implicit casting which is automatically performed by c# compiler because that data types are compatible with each other and destination variables has greater range then the source variables.
  • Notice, if i assign long data type to an int, short is assigned to byte and double is assigned to float; these will not be converted implicitly or automatically by c# compiler because i am trying to assigning great range data types to short range data types. At this stage a compile time error will be occurred. Fortunately i can still assign great range data type’s values to short range data type variables by using explicit or manual casting.
  • With explicit casting we can also perform type conversion / casting between incompatible data types such as convert / assign an int data type into a char data type.
  • Explicit casting is performing by a user / developer manually.
  • Some data or value can be lost during perform the explicit casting, because if we assign double data type’s value to an int then the fractional points of double can be lost because we are assigning floating points value to an integer type.
  • Explicit casting is very useful in some situations in C#.

Syntax:

data_type var_name = (target_type) source_type or expression;

STATEMENT DESCRIPTION
data_type This can be any data type such as string, int, char etc.
var_name Any valid or meaningful name.
= This is an assignment operator (=) which assigns or store a value to a variable (assign from right to left).
(target_type) This is the data type in which the expression or source data type will be converted or cast. Such as int, char, double etc.
source_type or expression; This is the actual data type or an expression which will be converted or cast, such as int i, double d, (i + d) etc.

Example, explicit or manual 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 byte data type variable.
            byte b;

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

         // Assign an int data type to a byte data type (b) by using an explicit casting/conversion.
            b = (byte)i;

         // Print byte b value, some value will be lost.
            Console.WriteLine("byte: {0} ", b);

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

         // Declare an int data type variable.
            int il;

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

         // Assign a long data type to an int data type (il) by using an explicit casting/conversion.
            il = (int)l;

         // Print an int il, no value will be lost.
            Console.WriteLine("int: {0} ", il);

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

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

         // Declare an int data type variable.
            int di;

         // Assign 54.22 value to double data type d1.
            d1 = 54.22;

         // Assign 55.2 value to double data type d2.
            d2 = 55.2;

         // Assign two double data type to an int data type (di) by using an explicit casting/conversion.
            di = (int)(d1 + d2);

         // Print int di, notice fractional points will be lost of double.
            Console.WriteLine("int: {0} ", di);

         // Declare an int data type variable.
            int ic;

         // Declare a char (character) data type variable.
            char ch;

         // Assign 3 value to an int (ic).
            ic = 3;

         // Assign an int data type to char data type (ch) by using an explicit casting/conversion.
            ch = (char)ic;

         // print char ch.
            Console.WriteLine("char: {0} ", ch);

            Console.ReadKey();

        } // 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 .